Powershell多凭证设置 [英] Powershell multiple credential setup

查看:117
本文介绍了Powershell多凭证设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是设置了 powershell 来收集一组服务器之间的磁盘空间信息.但是我遇到一个问题,当我尝试从一台服务器向另一台服务器进行身份验证时,它需要不同的凭据信息.

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.

例如SQLServer01

E.g. SQLServer01

ID:domain1 \ sqladmin1

ID: domain1\sqladmin1

PW:123456

PW: 123456

SQLServer02

SQLServer02

ID:domain2 \ sqladmin2

ID: domain2\sqladmin2

PW:654321

PW: 654321

现在,我设法以有限的Power-shell经验设置了第一个.

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
    
}

但是,我仅尝试为某些服务器输入另一个证书.在这种情况下为 domain2 \ .

Yet I am trying to input another credential for some servers only. domain2\ in this case.

Computer.txt作为参考

Computer.txt as reference

sqlserver1.domain1.com 
sqlserver2.domain1.domain2.com 
sqlserver3.domain1.com

包括"domain2"的一个需要多个证书.

the one including "domain2" would be the need of multiple credential.

推荐答案

如果在 Computers.txt 中使用计算机的完全限定域名,则可以使用简单的 if语句来确定要使用的域凭据.您只需要在下面的脚本( $ domain2Match ='.domain1.domain2.com')中将顶部的 $ domain2Match 变量更改为第二个域.

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
}

这篇关于Powershell多凭证设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆