2015-12-05 23 views
1

好的,所以這是我第一次在這裏發表,也是我第一次寫PowerShell。我的學校計算機維護和修理班請所有的學生,他們之一創建一個計算電腦內存的PowerShell腳本。我創建的計算機會計算總物理內存,用戶可用的總內存容量,以及計算機中的模塊以及每個模塊的數量。但是,在成功編寫代碼之後,我需要一些建議來調整代碼,以便它可以用於我的學校。如何在用戶輸入上重新啓動PowerShell程序

我的程序的第一部分打開並討論每條線的含義,遵循全部實體RAM,用戶可訪問的RAM,然後設置卡的方式。這導致正確的文字說關閉程序。我想添加的內容(順便說一下,我是PowerShell的初學者)是用戶重新運行應用程序的一種方式,如果程序中的任何變量都爲零(原因很明顯,計算機具有某種類型的RAM電腦正在運行)。現在它的一個Read-Host "Rerun memsrch ('y'/'n')?"

我想添加的另一件事是用戶能夠選擇,如果代碼是本地計算機或遙遠的機器。用戶然後可以通過IP或計算機名稱選擇計算機。以下是我現在的代碼,所以每個人都可以看到。

# Mesa Public Schools 
$mps="Mesa Public Schools Information Technology Services" 
$mps 

# User Help 
$print="The first section calculates your total physical memory, 
     the second line calculates the ram available to the user, 
     and the third line shows how the ram is divided up among 
     the ram cards.`n" 
$print 

#where I want to put a line of code to allow user to select if its local or remote 

$ram = get-wmiobject win32_computersystem | select totalPhysicalMemory 

Write-Host "Total usable RAM capacity" 
$ramOutput = get-wmiobject win32_computersystem | select totalPhysicalMemory | foreach {$_.totalPhysicalMemory} 

"RAM: " + "{0:N2}" -f ($ram.TotalPhysicalMemory/1GB) + "GB" 
Get-WMIObject -class win32_physicalmemory | Format-Table devicelocator, capacity -a 

Write-Host "Summary of System Memory" 
Get-WmiObject -class Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum 

# Coded BY 
$credits="Coded by Michael Meli" 
$credits 

#where I want to have the code reloop to the part of the code where 
#you first select if the computer is local or remote. 

Read-Host "Rerun memsrch (y/n)?" 

我也有一點與HTML 4.01和HTML 5的代碼的經驗,讓我明白了結構和參數的基礎知識,但除了PowerShell中的目前有很大一部分是在我頭上,所以沒有達到技術的原因,我不想讓我的大腦爆炸。 :P還要注意的代碼,如果運行Windows 8.1的計算機,但也必須與Windows 7兼容。這也不是我班級的成績,這是額外的功勞。

+1

你可能想要把或「作者「由邁克爾·梅利編碼」:邁克爾·梅利([email地址/ Twitter的手柄/比特幣的散列] )「在你的腳本中的[基於註釋的幫助]的NOTES域中(https://technet.microsoft.com/en-us/library/hh847834.aspx)部分 –

+0

我該怎麼做? –

+0

閱讀我鏈接的幫助文件,或者參閱PowerShell ISE –

回答

0
  1. 如果您將代碼包裝在function中,您將可以在需要時再次調用它。例如,如果第二個問題的用戶輸入是y

  2. 的計算機名或IP地址存儲用戶的輸入,這樣你就可以在WMI使用它調用你的腳本製作,與-ComputerName參數

示例代碼:

function Show-MemoryReport { 

    #... 

    #where I want to put a line of code to allow user to select if its local or remote 

    #if computer name is null (first pass) 
    if($computerName -eq $null) { 

     #ask the user 
     $computerName = Read-Host "Enter computer name or IP, or leave blank for local" 

     #if the string is empty, use the local computer name 
     if($computerName -eq "") { 
      $computerName = $env:COMPUTERNAME 
     } 
    } 

    $ram = Get-WmiObject -ComputerName $computerName -Class Win32_Computersystem | Select-Object totalPhysicalMemory 

    #... 

    #where I want to have the code reloop to the part of the code where you first select if the computer is local or remote. 

    $rerun = Read-Host "Rerun report (y/n)?" 

    if($rerun -eq "y") { Show-MemoryReport } 
} 

#at first run, make sure computer name will be asked 
$computerName = $null 

#run report 
Show-MemoryReport 

第一次通過後,$computerName不再是$null

提示:您不需要在變量中存儲字符串以便輸出它。只需將它寫在一個單獨的行上,如"print this on the screen",它將被輸出。

有關PowerShell的詳細信息構造和功能,你可以閱讀thisthis

+0

中的高級函數代碼片段中的示例。1.如果我選擇重新運行爲否,它是否會自動終止程序並啓動命令行? 2。如果我循環回到計算機名稱輸入所在的原始行,那麼代碼是否會再次爲同一臺計算機重新運行該行,或者是否必須重新插入該設置才能在該計算機上重新運行測試? 3.你是什麼意思「包裝我的代碼在一個函數」? –

+0

1.如果輸入任何不是'y'的程序將終止2.您將不得不再次輸入計算機名稱。 2.我將對我的代碼進行編輯,向您展示如何避免再次鍵入計算機名稱3.查看我的代碼:第一行有一個函數聲明,並且您的代碼被'{}'包圍。無論何時輸入函數名稱「Show-MemoryReport」,代碼都會運行。 – sodawillow

+0

所以現在輸入計算機名稱或IP,或運行本地,我有這個。 $ COMPUTERNAME =讀 - 主機 「輸入計算機名或IP,或留空本地」 \t如果($計算機名-eq $ NULL){ 如果($ COMPUTERNAME -eq 「」){$ 計算機名= $ env:COMPUTERNAME } –

相關問題