2013-12-20 39 views
0

如果我只是按下輸入沒有輸入任何變量..它會吐出錯誤。我可以添加什麼才能讓它再次被重新使用?Pompt如果沒有輸入輸入

mode con: cols=35 lines=5 
while (1) { 
    $tag1 = Read-Host 'Enter tag # or Q to quit' 
    if ($tag1 -eq "Q") { 
     break; 
    } 

    mode con: cols=80 lines=46 

    cls 

    sc.exe \\$tag1 start RemoteRegistry; 

    cls 

    start-sleep -seconds 2 

    cls 

    $OSInfo = get-wmiobject -class win32_operatingsystem -computername $tag1; 
    $OSInfo | Format-Table -Property @{Name="OS Name";Expression={$_.Caption}} -AutoSize; 
    $OSInfo | Format-Table -Property @{Name="System Boot Time";Expression={$_.ConvertToDateTime($_.LastBootUpTime)}} -AutoSize; 
    $OSInfo | Format-Table -Property @{Name="OS Install Date";Expression={$_.ConvertToDateTime($_.InstallDate)}} -AutoSize; 

    "`n" 

    "Current Date & Time: $(Get-Date -Format G)"; 

    "`n" 

    Get-WmiObject win32_computersystem -Computer $tag1 | Format-Table -Property @{Name="Username";Expression={$_.username}} -Autosize; 

    Get-EventLog system -computername $tag1 -InstanceId 2147489657 -Newest 10 | format-table EventID,TimeWritten,MachineName -AutoSize; 
} 

回答

1

幾種不同的方法:

$tag1 = $null 
while (-not $tag1) { 
    $tag1 = Read-Host 'Enter tag # or Q to quit' 
    if ($tag1 -eq "Q") { 
     return; 
    } 
} 

mode con: cols=80 lines=46 

cls 

sc.exe \\$tag1 start RemoteRegistry; 

cls 

start-sleep -seconds 2 

cls 

$OSInfo = get-wmiobject -class win32_operatingsystem -computername $tag1; 
$OSInfo | Format-Table -Property @{Name="OS Name";Expression={$_.Caption}} -AutoSize; 
$OSInfo | Format-Table -Property @{Name="System Boot Time";Expression={$_.ConvertToDateTime($_.LastBootUpTime)}} -AutoSize; 
$OSInfo | Format-Table -Property @{Name="OS Install Date";Expression={$_.ConvertToDateTime($_.InstallDate)}} -AutoSize; 

"`n" 

"Current Date & Time: $(Get-Date -Format G)"; 

"`n" 

Get-WmiObject win32_computersystem -Computer $tag1 | Format-Table -Property @{Name="Username";Expression={$_.username}} -Autosize; 

Get-EventLog system -computername $tag1 -InstanceId 2147489657 -Newest 10 | format-table EventID,TimeWritten,MachineName -AutoSize; 

或者:

$GetTag = { 
Switch (Read-Host 'Enter tag # or Q to quit') 
    { 
    'Q' {Return} 
    '' {.$GetTag} 
    default {$_} 
    } 
} 

$tag = &$GetTag 
+0

當我更換我的東西時發生錯誤。 – Aaron

+0

你使用了哪一個,你得到了什麼錯誤? – mjolinor

+0

我用兩個例子代替了第2-6行,並且我在表達式或語句中得到了意外的令牌'}'。 – Aaron

1

只需清除變量,然後循環,直到它被設置。

$tag1 = "" 
while (-not ($tag1)) { 
    $tag1 = Read-Host 'Enter tag # or Q to quit' 
} 
+0

我仍然得到錯誤。如果沒有輸入任何內容,我只想讓提示繼續詢問輸入,而不是繼續執行腳本。 – Aaron