继续查询Powershell开关参数
我想写一个Powershell模块,它可以读取我所在域中所有PC的信息。该脚本目前运行良好,但我想为Switch参数进行多个选择。示例:获取BERPCinfoFilter-Disk-Serial-Processor。如何调整开关参数的if查询,使其成为可能? 提前谢谢继续查询Powershell开关参数,powershell,if-statement,parameters,switch-statement,Powershell,If Statement,Parameters,Switch Statement,我想写一个Powershell模块,它可以读取我所在域中所有PC的信息。该脚本目前运行良好,但我想为Switch参数进行多个选择。示例:获取BERPCinfoFilter-Disk-Serial-Processor。如何调整开关参数的if查询,使其成为可能? 提前谢谢 function Get-BERPCinfoFilter { param( [switch]$Processor, [Switch]$Videocard, [Switch]
function Get-BERPCinfoFilter {
param(
[switch]$Processor,
[Switch]$Videocard,
[Switch]$Disk,
[Switch]$memory,
[Switch]$Serial
)
Write-Host Please wait...
#Search all PCs in Domain
$ADC = Get-ADComputer -SearchBase <#Ou from Domain#> | Select-Object -ExpandProperty Name
#Get informations from this PCs
$Function = ForEach ($Computer in $ADC){
$Computername = $Computer
$Connection = Test-Connection $Computername -Count 1 -Quiet
if($Connection -match "True"){
Write-Host "$Computername is reachable!" -ForegroundColor Green
#Processor
if($Processor -match "True"){
Get-CimInstance -ComputerName $Computername -Class Win32_Processor |
Select-Object -Property Name
}
#Videocard
if($Videocard -match "True"){
Get-CimInstance -ComputerName $Computername -Class Win32_VideoController |
Select-Object -Property Name,DriverVersion,@{Label="DriverDate";Expression={$_.DriverDate.ToString("dd.MM.yyyy")}}
}
#Disk
if($Disk -match "True"){
Get-CimInstance -ComputerName $Computername -Class Win32_LogicalDisk -Filter "DriveType=3" |
Select-Object DeviceID, VolumeName, @{n="Size in GB";e={[math]::truncate($_.size / 1GB)}}, @{n="FreeSpace in GB";e={[math]::truncate($_.freespace / 1GB)}}
}
#Memory
if($Memory -match "True"){
Get-CimInstance -ComputerName $Computername -class Win32_Physicalmemory |
Select-Object Devicelocator,@{n="Capacity in GB";e={[math]::truncate($_.capacity / 1GB)}}
}
#Serial
if($Serial -match "True"){
Get-CimInstance -ComputerName $Computername -class win32_bios |
Select-Object Serialnumber
}
}
if($Connection -match "False"){
Write-Host "$Computername is not reachable!" -ForegroundColor Red
}
}
$Pfad = "$env:USERPROFILE\Desktop\Result.csv"
$Function | Export-Csv -Path $Pfad -NoTypeInformation -Delimiter ";"
Write-Host "The result is saved at path: $Pfad" -ForegroundColor Yellow
}
您可以使用与[switch]参数类型无关的switch语句,而不是通过$PSBoundParameters上的名称:
您会注意到,我故意省略了为所有计算机查询Active Directory的部分
通过重构函数一次只查询一台计算机,您现在可以更灵活地重用它。对于查询所有计算机的现有用例,您仍然可以执行以下操作:
$AllComputers = Get-ADComputer
$Details = foreach($Computer in $AllComputers){
$Connection = Test-Connection $Computer.Name -Count 1 -Quiet
if($Connection){
Get-BERPCinfoFilter -ComputerName $Computer.Name -Processor -Disk -Whatever
}
}
$Details |Select property,names |Export-Csv ...
。。。但现在,您也可以对目标计算机进行更多选择,而不是总是等待函数查询所有计算机:
# Much faster if we only need info on one or a few machines
$computer1Memory = Get-BERPCinfoFilter -ComputerName computer01 -Memory
这是我带开关的新功能:
$Function= switch($PSBoundParameters.Keys){
#Processor
'Processor' {
Get-CimInstance -ComputerName $Computername -Class Win32_Processor |
Select-Object -Property Name
}
#Videocard
'Videocard' {
#$os = Get-WmiObject -ComputerName $Computername –Class Win32_OperatingSystem
Get-CimInstance -ComputerName $Computername -Class Win32_VideoController |
Select-Object -Property Name,DriverVersion,@{Label="DriverDate";Expression={$_.DriverDate.ToString("dd.MM.yyyy")}}
}
#Disk
'Disk' {
Get-CimInstance -ComputerName $Computername -Class Win32_LogicalDisk -Filter "DriveType=3" |
Select-Object DeviceID, VolumeName, @{n="Size in GB";e={[math]::truncate($_.size / 1GB)}}, @{n="FreeSpace in GB";e={[math]::truncate($_.freespace / 1GB)}}
}
#Memory
'Memory' {
Get-CimInstance -ComputerName $Computername -class Win32_Physicalmemory |
Select-Object Devicelocator,@{n="Capacity in GB";e={[math]::truncate($_.capacity / 1GB)}}
}
#Serial
'Serial' {
Get-CimInstance -ComputerName $Computername -class win32_bios |
Select-Object Serialnumber
}
}
$Function | select * | Export-Csv -Path $Pfad -NoTypeInformation -Delimiter ";"
一旦你实现了这一点,你可以通过运行作业一次返回多台计算机。。。如果您想插入开关盒,但它只能输出一个属性。@KilianP。它们正在被输出,您只是看不到它们,因为PowerShell的格式化子系统只显示在结果集合的第一项上找到的属性。请尝试$Details | Select*@MathiasR.Jessen,| Select*只获取名称,而不获取over属性。@KilianP。为了清楚起见,您替换了代码以获取。。。示例中的注释与实际代码一致,对吗?
$Function= switch($PSBoundParameters.Keys){
#Processor
'Processor' {
Get-CimInstance -ComputerName $Computername -Class Win32_Processor |
Select-Object -Property Name
}
#Videocard
'Videocard' {
#$os = Get-WmiObject -ComputerName $Computername –Class Win32_OperatingSystem
Get-CimInstance -ComputerName $Computername -Class Win32_VideoController |
Select-Object -Property Name,DriverVersion,@{Label="DriverDate";Expression={$_.DriverDate.ToString("dd.MM.yyyy")}}
}
#Disk
'Disk' {
Get-CimInstance -ComputerName $Computername -Class Win32_LogicalDisk -Filter "DriveType=3" |
Select-Object DeviceID, VolumeName, @{n="Size in GB";e={[math]::truncate($_.size / 1GB)}}, @{n="FreeSpace in GB";e={[math]::truncate($_.freespace / 1GB)}}
}
#Memory
'Memory' {
Get-CimInstance -ComputerName $Computername -class Win32_Physicalmemory |
Select-Object Devicelocator,@{n="Capacity in GB";e={[math]::truncate($_.capacity / 1GB)}}
}
#Serial
'Serial' {
Get-CimInstance -ComputerName $Computername -class win32_bios |
Select-Object Serialnumber
}
}
$Function | select * | Export-Csv -Path $Pfad -NoTypeInformation -Delimiter ";"