Use PowerShell
The Shell Is Calling
The Shell Is Calling
Jeffery Hicks recently talked about the Param statement in his Prof. Powershell column for MCP Magazine (Keeping Your Options Open). If you are new to PowerShell, I would definitely recommend reading the article, as provides a nice introduction to the Param statement.
Jeffrey recommended to cast your parameter as the type of object that you are expecting, as way to catch errors. I wholeheartedly agree. That can cause trouble though, if you want to allow consumers of your script to pass in one or more values for that parameter. In the spirit of the article’s title, I thought I would share a method I’ve come across to handle that situation.
Jeffrey gave is the below function.
Function Check-Service {
Param([string]$computername=$env:computername,
[string]$service=”spooler” )
$wmi=get-wmiobject win32_service -filter “name=’$service’” -computername $computername
if ($wmi.state -eq “running”) {
write $True
}
else {
write $False
}
}
Now, you can easily see where you might want to check for a service on multiple computers, but as this function stands, we would have to call it one time for each computer we want to check.
We can modify this script to take one or more computer names simply by changing the cast for the $computername variable from [string] to [string[]]. That changes makes our variable hold an array of strings, rather than just one.
We don’t have to change the default value, as PowerShell handles the details of converting that value to an array.
Since the –ComputerName parameter for Get-WMIObject can take an array of computer names, we don’t have to change much else in the function.
I do add a foreach loop for the reporting end of the script, so we can see which machines have the service running.
And I end up with this (my changes in bold):
Function Check-Service
{
Param( [string[]]$computername=$env:computername, [string]$service=”spooler” )
$wmi=get-wmiobject win32_service -filter “name=’$service’” -computername $computername
foreach ($one in $wmi)
{
if ($one.state -eq “running”)
{
write $one.SystemName, $true
}
else
{
write $one.SystemName, $False
}
}
}
PSMDTAG:FAQ param
August 4, 2009 - 8:31 am
HI, I have a question regarding passing the parameters to a function like this:
function WriteVerifyLog($message, $logFile){
[string]$m=$message;
if ($m.contains(‘successfully’)) {continue}
else{
Add-Content -Value $message -Path $LogFile;
}
}
when I call the function:
WriteVerifyLog($result, $logPreInstall);
draws my attention that $message contains both the value of $result AND $logPreInstall. I don’t know what’s the reason, and I like to ask your feedback on that. I’d expect that $logFile would take the value of $logPreInstall. what am I doing wrong? Thanks in advance for the reply.
August 4, 2009 - 9:23 am
Orlando,
The reason that both parameters are being passed as one is that PowerShell does not use the parens “()” to enclose parameters (that is only method calls from objects). Your function is only seeing one parameter being passed – an array containing $result and $logPreInstall.
Try changing your function call to
WriteVerifyLog $result $logPreInstall
and you should see the desired results.
November 10, 2011 - 11:20 pm
Thanks for the tip about “Passing Parameters” One of the points raised in the post was about passing a text datatype parameter to a procedure called in the background and the fact that the contents of the text variable are lost. The same code called in the foreground works at both parameters are being passed as one is that PowerShell does not use the parens “()” to enclose parameters (that is only method calls from objects).Thanks Steven Murawski.