2016-09-30 147 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 
    #> 
    } 
} 

注意,如果安装程序已经在目标,不知道这是预期的行为或不,这将无法安装该产品。