2016-09-30 108 views
0

我想弄清楚如何編寫一個PowerShell腳本,將自動安裝Office2010在多個電腦上。我在創建文本文件的部分掙扎,我們通過列出ComputerName和用戶登錄循環。我已經在網絡上研究了這些,但由於某種原因我無法使這個工作。Powershell腳本來遠程安裝軟件(微軟Office)

Function Get-FileName{ 
[CmdletBinding()] 
Param(
    [String]$Filter = "|*.*", 
    [String]$InitialDirectory = "C:\") 

    [void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") 
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog 
    $OpenFileDialog.initialDirectory = $InitialDirectory 
    $OpenFileDialog.filter = $Filter 
    [void]$OpenFileDialog.ShowDialog() 
    $OpenFileDialog.filename 
} 


ForEach ($computer in (GC (Get-FileName -InitialDirectory $env:USERPROFILE\Desktop -Filter "Text files (*.txt)|*.txt|All files (*.*)|*.*"))) { 

$filepath = Test-Path -Path "\\$computer\C:\Program Files (x86)\Microsoft Office" 
    If ($filepath -eq $false) 
    { 
Get-Service remoteregistry -ComputerName $computer | Start-Service 
    Copy-Item -Path "\\server\Orig\Install\Office2010" -Destination "\\$computer\c$\windows\temp\" -Container -Recurse -Force 
    # $InstallString = '"C:\windows\temp\Office 2010\setup.exe"' 
    # ([WMICLASS]"\\$computer\ROOT\CIMV2:Win32_Process").Create($InstallString) 

    # "$computer" + "-" + "(Get-Date)" | Out-File -FilePath "\\server\Orig\Install\RemoteInstallfile.txt" -Append 
    # } 
    # Else 
    # { 
    # "$computer" + "_Already_Had_Software_" + "(Get-Date)" | Out-File -FilePath "\\server\Orig\Install\RemoteInstallfile.txt" -Append 
    } 
} 

ComputerList.txt

IT-Tech | David 

IT科技將是計算機名和大衛將是用戶。然後,我會在txt文件中逐行顯示一個列表。

所以我想我可以做這樣的事情列出計算機名稱,然後是如何安裝的用戶名。這部分雖然只是試圖學習和看看這個PowerShell的東西是什麼,但讓我感到困惑!

任何幫助,將不勝感激!

回答

1

正如您所說,您的文件的某一行將包含類似「IT-Tech | David」的內容,因此,當您通過該文件進行迭代時,該值爲$computer。然後,您嘗試將其用作計算機名稱調用,這當然會失敗,因爲首先需要將其拆分。

我也會指出在腳本中縮寫和使用別名是非常糟糕的形式,您應該只在控制檯中使用它們。同樣爲了可讀性,它有助於將複雜的比特分離出來。

$file = Get-FileName -InitialDirectory $env:USERPROFILE\Desktop -Filter "Text files (*.txt)|*.txt|All files (*.*)|*.*" 
ForEach ($item in (Get-Content $file)) { 
    $sitem = $item.Split("|") 
    $computer = $sitem[0].Trim() 
    $user = $sitem[1].Trim() 

    $filepath = Test-Path -Path "\\$computer\C:\Program Files (x86)\Microsoft Office" 
    If ($filepath -eq $false) 
    { 
    Get-Service remoteregistry -ComputerName $computer | Start-Service 

    Copy-Item -Path "\\server\Orig\Install\Office2010" -Destination "\\$computer\c$\windows\temp\" -Container -Recurse -Force 

    <# 
    $InstallString = '"C:\windows\temp\Office 2010\setup.exe"' 
    ([WMICLASS]"\\$computer\ROOT\CIMV2:Win32_Process").Create($InstallString) 

    "$computer" + "-" + "(Get-Date)" | Out-File -FilePath "\\server\Orig\Install\RemoteInstallfile.txt" -Append 
    } 
    Else 
    { 
    "$computer" + "_Already_Had_Software_" + "(Get-Date)" | Out-File -FilePath "\\server\Orig\Install\RemoteInstallfile.txt" -Append 
    #> 
    } 
} 

注意,如果安裝程序已經在目標,不知道這是預期的行爲或不,這將無法安裝該產品。