我有一個腳本將文件從服務器複製到c:\windows\temp\
以安裝Microsoft 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
}
$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\Install\Office2010" -Destination "\\$computer\c$\windows\temp\" -Container -Recurse -Force
<#
$InstallString = '"C:\windows\temp\Office2010\setup.exe"'
([WMICLASS]"\\$computer\ROOT\CIMV2:Win32_Process").Create($InstallString)
"$computer" + "-" + "$(Get-Date)" | Out-File -FilePath "\\server\Install\Office2010\RemoteInstallfile.txt" -Append
#>
} Else {
"$computer" + "_Already_Had_Software_" + "$(Get-Date)" | Out-File -FilePath "\\server\Install\Office2010\RemoteInstallfile.txt" -Append
}
}
在想,如果我可以插入下方某處片在此代碼,然後繼續前進,刪除此文件夾?
$folderToDelete = "\\$computer\c$\windows\temp\"
$ErrorActionPreference= 'silentlycontinue'
[io.directory]::delete($folderToDelete, $true)
$fso = New-Object -ComObject scripting.filesystemobject
$fso.DeleteFolder($folderToDelete,$true)
if (Test-Path ($folderToDelete)) {
New-Item -ItemType directory -Path .\EmptyFolder
robocopy .\EmptyFolder $folderToDelete /mir
Remove-Item .\EmptyFolder
Remove-Item $folderToDelete
}
但我不確定在刪除之前安裝是否完成?有沒有人做過這樣的事情,並希望分享一些指導?
你可以從你用來啓動安裝程序的WMI方法中獲取PID,而不是Get-process -id $ thatpid,請等待 –