问题
I just setup a powershell to collect disk space info among a group of servers. but I encounter an issue that while I try to authenticate from one server to another, it require different credential info.
E.g. SQLServer01
ID: domain1\sqladmin1
PW: 123456
SQLServer02
ID: domain2\sqladmin2
PW: 654321
right now i manage to setup first one with limited power-shell experience.
$comp= Get-Content "C:\disk_info\Computers.txt"
$diskvalue = @()
#$cre = get-Credential
$username = "domain1\sqladmin1"
$password = "123456"
$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $secureStringPwd
foreach($pc in $comp)
{
$diskvalue += Get-WmiObject -Class Win32_logicaldisk -ComputerName $pc -credential $creds -Filter DriveType=3 |
Select SystemName , DeviceID , @{Name=”size(GB)”;Expression={“{0:N1}” -f($_.size/1gb)}}, @{Name=”freespace(GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}}, @{Name=”UsedSpace(GB)”;Expression={“{0:N2}” -f(($_.size - $_.FreeSpace)/1gb)}}
#$diskvalue -replace ":",""
$diskvalue | Export-Csv C:\disk_info\DiskReport.csv -NoTypeInformation
}
Yet I am trying to input another credential for some servers only. domain2\
in this case.
Computer.txt as reference
sqlserver1.domain1.com
sqlserver2.domain1.domain2.com
sqlserver3.domain1.com
the one including "domain2" would be the need of multiple credential.
回答1:
If you are using the full qualified domain names for the machine in Computers.txt
, you could use a simple if
statement to decide which domain credentials to use. You will just need to change the $domain2Match
variable at the top to your 2nd domain in the below script ($domain2Match='.domain1.domain2.com'
).
$comp= Get-Content "C:\disk_info\Computers.txt"
$diskvalue = @()
# Put your FQDN without the server name here for the seconded domain
$domain2Match = '.domain1.domain2.com'
# Credential 1
$username = "domain1\sqladmin1"
$password = "123456"
$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $secureStringPwd
# Credential 2
$username2 = "domain2\sqladmin2"
$password2 = "123456"
$secureStringPwd2 = $password2 | ConvertTo-SecureString -AsPlainText -Force
$creds2 = New-Object System.Management.Automation.PSCredential -ArgumentList $user2, $secureStringPwd2
foreach($pc in $comp)
{
$credsToUse = $null
If($pc -imatch $domain2Match){
# Matched use domain 2 Credential
$credsToUse = $creds2
}Else {
# No match use domain 1 Credential
$credsToUse = $creds
}
$diskvalue += Get-WmiObject -Class Win32_logicaldisk -ComputerName $pc -credential $credsToUse -Filter DriveType=3 |
Select SystemName , DeviceID , @{Name=”size(GB)”;Expression={“{0:N1}” -f($_.size/1gb)}}, @{Name=”freespace(GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}}, @{Name=”UsedSpace(GB)”;Expression={“{0:N2}” -f(($_.size - $_.FreeSpace)/1gb)}}
$diskvalue | Export-Csv C:\disk_info\DiskReport.csv -NoTypeInformation
}
回答2:
You can create two Credenital Objects:
Cred 1
$username = "domain1\sqladmin1"
$password = "123456"
$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $secureStringPwd
Cred 2
$username = "domain2\sqladmin2"
$password = "123456"
$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$creds2 = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $secureStringPwd
Then use an IF Statement along with splatting Params (will be easier and cleaner)
foreach($pc in $comp)
{
$Params = @{
Class = "Win32_logicaldisk"
ComputerName = $pc
Credential = $creds
Filter = 'DriveType=3'
}
If ($pc -eq "SQLServer02")
{
$Params["Credential"] = $creds2
}
$diskvalue += Get-WmiObject @Params | Select SystemName , DeviceID , @{Name=”size(GB)”;Expression={“{0:N1}” -f($_.size/1gb)}}, @{Name=”freespace(GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}}, @{Name=”UsedSpace(GB)”;Expression={“{0:N2}” -f(($_.size - $_.FreeSpace)/1gb)}}
#$diskvalue -replace ":",""
$diskvalue | Export-Csv C:\disk_info\DiskReport.csv -NoTypeInformation
}
来源:https://stackoverflow.com/questions/38953487/powershell-multiple-credential-setup