windows - Powershell - Query remote registry key value and generate text file IF value equals 1 -
first time posting. thoroughly enjoy site , of talent stops in out.
i have slopped bits of powershell remote query list of machines, stored in .csv file, registry value. if registry key's value equal '1', script should create text file using machine's name name of text file.
everything works great. script runs happily without errors. problem when go , remotely check targeted registry value, find value isn't 1. script creating file every line in .csv.
what doing wrong?
edit*** found a problem had typo in $key variable registry path. 7/17/2013 2:21p
$file = import-csv 'c:\temp\machines.csv' foreach ($line in $file) { $machinename = $line.machinename trap [exception] {continue} $reg = [microsoft.win32.registrykey]::openremotebasekey("localmachine",$machinename) $key = "software\\microsoft\\windows nt\\currentversion\\winlogon" $regkey = "" $regkey = $reg.opensubkey($key) $keyvalue = "" $keyvalue = $regkey.getvalue('autoadminlogon') if ($keyvalue = "1") { try { $textfile = new-item -path "c:\temp\autologin" -name $machinename -itemtype "file" } catch { $msg = $_ $msg } } $results = $machinename , $keyvalue write-host $results #output below here: }
in powershell =
assignment operator, not comparison operator. change line:
if ($keyvalue = "1")
into this:
if ($keyvalue -eq "1")
for more information see get-help about_operators
.
you're making way complicated, btw. should suffice:
$keyname = 'software\\microsoft\\windows nt\\currentversion\\winlogon' import-csv 'c:\temp\machines.csv' | % { $reg = [microsoft.win32.registrykey]::openremotebasekey("localmachine", $_.machinename) $key = $reg.opensubkey($keyname) $value = $key.getvalue('autoadminlogon') if ($value -eq "1") { $filename = join-path "c:\temp\autologin" $_.machinename try { touch $filename $textfile = get-item $filename } catch { $_ } } write-host ($_.machinename , $value) }
Comments
Post a Comment