2015-10-17 80 views
-1

我想獲取用戶輸入的目標計算機,我可以運行此腳本。如果沒有指定計算機名稱,我想顯示一條錯誤消息。恩。我選擇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,看它是否有效。如果這使得現場。 –

回答

0

您需要將您的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機器的存在目錄