Likely Variable Scope Issue
i've done several tests , haven't been able figure out why variable not being recognized in invoke-command (it works @ other levels)...i've tested independently , works i'm not sure i'm missing. can set me straight.
if ($type -eq 's' -or $type -eq 's') { $software = (read-host "enter software keyword: ") foreach ($server in $servers) { if (test-connection -computername $server -count 1 -quiet) { $server invoke-command -computername $server { $software get-wmiobject -class win32_product | where-object -filterscript {$_.name -like "*$software*"} | format-table -property name,version -autosize } #end invoke } #end if else {write-host "could not connect: $server"} } #end foreach } #end elseif
the variable i'm having trouble $software.
i tested works using powershell v3... did copy code posted?
at point error? when run $scriptblock=... part?
$software='hp'
$scriptblock=[scriptblock]::create("get-wmiobject -class win32_product | where-object -filterscript {`$_.name -like '*$($software)*'} |format-table -property name,version -autosize")
get-variable scriptblock | select -expandproperty value
#$scriptblock contains:
get-wmiobject -class win32_product |
where-object -filterscript {$_.name -like '*hp*'} |
format-table -property name,version -autosize
are using version 2?
just confirm... ` character in `$_.name ascii code 96, not ' ascii code 39
i've tried above on computer powershell v2... creates same result as running on v3.
inspired carlsberg.
Windows Server > Windows PowerShell
Comments
Post a Comment