我正在尝试创建一个PowerShell脚本,远程检查域中的每台计算机上是否有注册表项,然后将该键值与计算机名一起输出到.csv文件。
到目前为止,脚本将域中的所有计算机输出到.csv文件,但是将其本地注册表项值设置为远程计算机的值。
非常感谢任何帮助,这是我到目前为止所做的。
Import-Module ActiveDirectory
$SRVS = Get-ADComputer -Filter * -SearchBase 'DC=mydomain,DC=local' |
select dnshostname
foreach ($SRV in $SRVS) {
$REG = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $SRV.name)
$REGKEY = $REG.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\QualityCompat")
$MELT = $REGKEY.GetValue('cadca5fe-87d3-4b96-b7fb-a231484277cc')
"$($SRV);$($MELT)" | Out-File C:\Users\user1\Desktop\regkeys.CSV -Append
}
答案 0 :(得分:2)
声明
$SRVS = Get-ADComputer ... | select dnshostname
为您提供仅包含一个属性的自定义对象列表:dnshostname
。但是在你的循环中,你试图使用属性name
,这些自定义对象没有。因此声明
[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $SRV.name)
有效地成为
[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $null)
表示您打开本地注册表,而不是远程主机上的注册表。
将$SRV.name
更改为$SRV.dnshostname
,问题就会消失。
答案 1 :(得分:1)
一旦它被实例化,RegistryKey
类就不会暴露它是一个远程密钥。这意味着您必须自己记录计算机名称。远程注册表值也没有标准格式。
如果我有PowerShell v5 +,我会使用类似的东西:
Import-Module ActiveDirectory
# No need for the Select-Object here since we're using $SRV.Name later
$SRVS = Get-ADComputer -Filter * -SearchBase 'DC=mydomain,DC=local'
# Create an arraylist to save our records
$Report = New-Object System.Collections.ArrayList
# This try finally is to ensure we can always write out what we've done so far
try {
foreach ($SRV in $SRVS) {
# Test if the remote computer is online
$IsOnline = Test-Connection -ComputerName $SRV.Name -Count 1 -Quiet;
if ($IsOnline) {
# If system is Online
$REG = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $SRV.name)
$REGKEY = $REG.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\QualityCompat")
$MELT = $REGKEY.GetValue('cadca5fe-87d3-4b96-b7fb-a231484277cc')
# Create a PSObject record for convenience
$Record = [PSCustomObject]@{
ComputerName = $SRV;
Key = $REGKEY.Name;
Value = $MELT;
}
}
else {
# If system is Offline
# Create a PSObject record for convenience
$Record = [PSCustomObject]@{
ComputerName = $SRV;
Key = '';
Value = '';
}
}
# Add our record to the report
$Report.Add($Record);
}
}
finally {
# Always write out what we have whether or not we hit an error in the middle
$Report | Export-Csv -Path "C:\Users\user1\Desktop\regkeys.csv" -NoTypeInformation
}
这可能适用于PowerShell v3 +,但我不再需要它来测试。
答案 2 :(得分:0)
你是否有任何理由打印出实际的regkey而只是检查它的存在?
它存在或不存在。说使用像...这样的东西。
vowels = ['a' , 'e' , 'i' , 'o' , 'u'] # this declares a list of vowels
word = "milliways" # declares an input word
found = [] # used to store list of vowels in word
for letter in word: # iterates through every character in word
if letter in vowels: # checks if character is in the vowels list
if letter not in found:
found.append(letter) # if the character is a vowel, and not in found list it adds it
for vowel in found: # prints list of vowels in word
print(vowel)