Powershell,获取计算机名称的用户输入并显示该计算机BIOS信息,操作系统信息和磁盘信息

问题描述:

我想获取用户输入的目标计算机,我可以运行此脚本。如果没有指定计算机名称,我想显示一条错误消息。恩。我选择dc1和所有的BIOS信息,Os信息和硬盘显示为dc1而不是我的本地计算机。关于如何实现这一点的任何想法?Powershell,获取计算机名称的用户输入并显示该计算机BIOS信息,操作系统信息和磁盘信息

#Clears the screen 
cls 

#Sets status to 0 just incase its not already zero, option 4 sets status to 4 and exit the loop 
$status = 0 

#Gets the BIOS information 
$biosInfo = get-CIMInstance -Class CIM_BIOSElement |select-Object SMBIOSBIOSVersion, Manufacturer, SerialNumber, Version | Format-Table 

#Gets the Operating System information 
$osInfo = get-CIMInstance -Class CIM_OperatingSystem | Select-Object Caption, Version |Format-List 

#Gets the Disk information 
$discInfo= get-CIMInstance -Class CIM_LogicalDisk |Select-Object DeviceID, FreeSpace, Size |Format-List @{Name=‘DeviceID‘;Expression={$_.DeviceID}}, @{Name=‘FreeSpace(InPercent)’;Expression={[math]::Round($_.FreeSpace/$_.Size,2)*100}}, @{Name=‘Size(GB)’;Expression={[int]($_.Size/1GB)}}            

#Function to select computer name 
function name { 

$a = get-cimInstance -Class CIM_ComputerSystem.computername 

} 


#Menu selection zone! 
Write-Host "Query Computer System Main Menu" 
Write-Host "" 
Write-Host "1 Display Current BIOS information" 
Write-Host "2 Display Operating System Information" 
Write-host "3 Display Hard Disk Information" 
Write-Host "4 Exit" 
Write-Host "" 

do 
{ 

$status = read-host "Please enter and Option" 
$computer = Read-Host "Enter target computer name" 

if ($computer -ne $computer){Write-host "ERROR! Please enter a target computer name" } 
if ($status -ne 4){ 
} 

#Where the menu magic happens 
switch ($status){ 
    1{ $biosInfo ; pause} 
    2{ $osInfo ; pause} 
    3{ $discInfo ; pause} 
    4{ $status_text = '4 Exit' ;$status = 4} 
    default {"Please select a number in the list!"} 
} 
} 
#Exits the do loop when status is equal to 4 
while ($status -ne 4) 
+0

我想出了错误部分,但我仍然需要弄清楚如何选择在我的域中的计算机。 –

+0

你是什么意思选择一个在你的域名的计算机?如果检查AD是否有效? – Matt

+0

如果我输入dc1,我想显示dc1的统计数据,是的,我想检查AD,看它是否有效。如果这使得现场。 –

您需要将您的Get-CimInstance调用抽象为函数或参数化的脚本块。

取而代之的是,它会立即执行对你的本地机器:

$OSInfo = Get-CimInstance -Class CIM_OperatingSystem 

你想是这样的:

$OSInfoGetter = { 
    param([string]$ComputerName) 

    Get-CimInstance -Class CIM_OperatingSystem -ComputerName $ComputerName 
} 

现在,您可以与呼叫操作重新调用您的脚本块(& )并提供任何计算机名称作为参数:

$OSInfo = &$OSInfoGetter $env:ComputerName 

对于检查计算机名,用户输入,我把另一do{}while()循环中的第一个内:

do 
{ 
    $status = Read-Host "Please enter and Option" 

    do{ 
     $Computer = Read-Host "Enter target computer name" 
     if (($InvalidName = [string]::IsNullOrWhiteSpace($Computer))){ 
      Write-Warning "ERROR! Please enter a target computer name" 
     } 
    } until (-not $InvalidName) 

    # switch goes here 

} while ($status -ne 4) 

您也可以替换你想有任何逻辑,比如检查在Active机器的存在目录