简体   繁体   中英

Powershell Script to Remotely Install Software (Microsoft Office)

I am trying to figure out how to write a powershell script that will automatically install office2010 on multiple pcs. I am struggling on the portion where you create the text file that we loop through listing the ComputerName and the Users Login. I have researched this all over the web but for some reason am unable to get this to work.

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-Tech would be the computer name and David would be the user. Then I would have a list like this line by line in the txt file.

So i was thinking I could do something like this Listing the computer name and then the user name of how to install. This part confuses me though just trying to learn and see what this powershell stuff is all about!

Any help with this would be greatly appreciated!

A line of your file, as you've said, will contain something like "IT-Tech | David", so when you iterate through that file that's the value of $computer . You then attempt to use this as the computer name call which will of course fail because first you need to split it out.

I will also point out it is extremely bad form to abbreviate and use aliases in scripts, you should only use them in the console. Also for readability it helps to split complex bits out.

$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
    #>
    }
}

Note that this will NOT install the product if the installer is already in the destination, not sure if that is intended behaviour or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM