powershell修改远程计算机名
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell修改远程计算机名相关的知识,希望对你有一定的参考价值。
参考技术A 备注:#将域控账号保存到脚本中,减少手指的工作量(这个是运维的基本素质)!!!!
$UserName = "域控管理员账号@XXXX.com"
$PassWord = ConvertTo-SecureString -String "域控管理员密码" -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $UserName,$PassWord
#远程控制
Invoke-Command -ComputerName 远程计算机名 -ScriptBlock
$UserName = "域控管理员账号@XXXX.com"
$PassWord = ConvertTo-SecureString -String "域控管理员账号" -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $UserName,$PassWord
#修改命令在下方(这个命令可以随意修改成你喜欢的例如开放端口或者关闭增加权限)
Rename-Computer -NewName 计算机新名称 -DomainCredential $Credential
Add-LocalGroupMember -Group "组名称" -Member 域控/成员
-Credential $Credential
远程注册表项提取程序PowerShell脚本
我正在尝试创建一个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("SOFTWAREMicrosoftWindowsCurrentVersionQualityCompat")
$MELT = $REGKEY.GetValue('cadca5fe-87d3-4b96-b7fb-a231484277cc')
"$($SRV);$($MELT)" | Out-File C:Usersuser1Desktop
egkeys.CSV -Append
}
答案
一旦它被实例化,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("SOFTWAREMicrosoftWindowsCurrentVersionQualityCompat")
$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:Usersuser1Desktop
egkeys.csv" -NoTypeInformation
}
这可能适用于PowerShell v3 +,但我不再需要它来测试。
另一答案
该声明
$SRVS = Get-ADComputer ... | select dnshostname
为您提供仅包含一个属性的自定义对象列表:dnshostname
。但是在你的循环中,你试图使用属性name
,这些自定义对象没有。因此声明
[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $SRV.name)
有效地成为
[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $null)
意味着您打开本地注册表,而不是远程主机上的注册表。
将$SRV.name
改为$SRV.dnshostname
,问题就会消失。
另一答案
你试图打印出实际的regkey与仅检查它是否存在的任何原因?
它存在或不存在。说用...之类的东西
Clear-Host
Import-Module ActiveDirectory
$SRVS = (Get-ADComputer -Filter * -SearchBase (Get-ADDomainController).DefaultPartition)
$MeltHive = 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionQualityCompat'
$MeltHiveKey = 'cadca5fe-87d3-4b96-b7fb-a231484277cc'
ForEach($Srv in $SRVS)
{
Invoke-Command -ComputerName $Srv.Name -ScriptBlock {
If (Get-ItemProperty -Path $Using:MeltHive -Name $MeltHiveKey -ErrorAction SilentlyContinue)
{"On Host $env:COMPUTERNAME MELT registry information exists"}
Else {Write-Warning -Message "On host $env:COMPUTERNAME MELT registry information does not exist"}
}
}
ForEach($Srv in $SRVS)
{
Invoke-Command -ComputerName $Srv.Name -ScriptBlock {
If ((Get-ChildItem -Path HKLM:SOFTWAREMicrosoftWindowsCurrentVersion) -match 'QualityCompat')
{"On Host $env:COMPUTERNAME MELT registry information exists"}
Else {Write-Warning -Message "On host $env:COMPUTERNAME MELT registry information does not exist"}
}
}
Results of both the above is:
WARNING: On host DC01 MELT registry information does not exist
WARNING: On host EX01 MELT registry information does not exist
WARNING: On host SQL01 MELT registry information does not exist
On Host IIS01 MELT registry information exists
以上是关于powershell修改远程计算机名的主要内容,如果未能解决你的问题,请参考以下文章
powershell v2.0 从批处理 cmd 文件传递变量计算机名参数