Specifying ComputerName and Credential Parameters

When creating custom scripts, you specify the target type in the script metadata. Keep the target type in mind when writing the script. The primary thing to consider when writing the script will be whether you should include the ComputerName and Credential parameters on the commands. The ITScripts environment provides the computer name in the ST_ComputerName variable and the credential for that computer in the ST_Credential variable. For example, when writing a script with target type of “Any” you might specify this command:

Get-WmiObject win32_Share -ComputerName $ST_ComputerName -Credential $ST_Credential

However, this command will fail if run against the local machine with the following error:

Get-WmiObject : User credentials cannot be used for local connections

In other words, this command will work if the target type is “Any” and the target machine is not the console, but it will fail in the following cases:

  • Target type is ‘Console’
  • Target type is ‘WinRM Remoting’
  • Target type is ‘Any’ and the target is the console computer

To take these cases into account, your script may need to test whether it is using remoting, or if the target is the console machine. To simplify script creation, ITScripts provides two functions: ST-ComputerAndCredential and ST-SubCC.


This function returns a string containing the appropriate string to use for the

-ComputerName and -Credential parameters based on the type of script, whether it is executing on the console, and whether credentials were provided for the machine. It will return one of these strings:

“-ComputerName $ST_ComputerName –Credential $ST_Credential”

“-ComputerName $ST_ComputerName”


You can use this in your script command without concern for the environment you are executing in. For example:

$cc = ST-ComputerAndCredential

$shares = Invoke-Expression "Get-WmiObject win32_Share $cc"

Invoke-Expression is used in order to substitute the value of $cc before executing the command. The issue with this construct is that you must remember to escape special characters in your command. For example, the “$” and quote characters need to be escaped in the following command:

$ST_CC = ST-ComputerAndCredential

$registryObject = Invoke-Expression "Get-WmiObject -list

-namespace root\default $ST_CC |

where-object { `$_.name -eq `"StdRegProv`" }"

The following function makes this even simpler.


This function combines the two commands shown in the previous example. The first time it is invoked, it puts the result of ST-ComputerAndCredential into a PowerShell variable named ST_CC. It then passes the resulting command to Invoke-Expression. The thing to remember when using ST_SubCC is to place the command inside single quotes, not double quotes. The previous examples would be:

$shares = ST-SubCC ‘Get-WmiObject win32_Share $ST_CC’


$registryObject = ST-SubCC ‘Get-WmiObject -list

-namespace root\default $ST_CC |

where-object { $_.name -eq "StdRegProv" }’