Powershell multiple credential setup

北慕城南 提交于 2021-01-29 04:11:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!