WMI commands can either receive explicit credentials as an argument (the -Credential
flag), or run in the security context of the user running the script if no credentials are provided.
Right now, my script looks like this:
if ($Creds) { # if the user provided credentials
Invoke-WMIMethod -Credential $Creds <...something...>
... hundreds more lines of Invoke-WMIMethod code ...
else { # user did not supply credentials, use current security context
Invoke-WMIMethod <...something...>
... same exact hundreds of lines of Invoke-WMIMethod code, without -Credential ....
}
In other words, the only difference is the -Credential
flag. Is there any way I can consolidate this huge if-else into one block of code?