1
我有一個文件夾中的.msu和.exe文件。我爲每個創建INI文件。對於.msu文件,它運行得很好。對於.exe文件,它處理第一個文件,然後跳過其餘的文件。代碼powershell變量更新的.msu文件,但不是.exe文件
$INIFileName = ($strItem -replace ".$fileExtention",".ini")
似乎是問題所在。它不會取代任何初始.exe文件
輸出繼.exe文件的一些推廣該.ini格式:
** Creating .INI files for any msu's that don't have one **
Original file name with .exe: kb0000001.msu
New INI file name with .ini : kb0000001.ini
Original file name with .exe: kb0000002.msu
New INI file name with .ini : kb0000002.ini
Original file name with .exe: kb0000003.msu
New INI file name with .ini : kb0000003.ini
** Creating .INI files for any exe's that don't have one **
Original file name with .exe: kb0000004.exe
New INI file name with .ini : kb0000004.ini
Original file name with .exe: kb0000005.exe
New INI file name with .ini : kb0000005.exe
C:\Temp\PatchBundle\kb0000005.exe exists. Skipping.
Original file name with .exe: kb0000006.exe
New INI file name with .ini : kb0000006.exe
C:\Temp\PatchBundle\kb0000006.exe exists. Skipping.
PS C:\temp\PatchBundle>
測試文件:(代碼創建)
kb0000001.msu
kb0000002.msu
kb0000003.msu
kb0000004.exe
kb0000005.exe
kb0000006.exe
代碼:
## Create test files
Function CreateTestFiles ($strDestPath)
{
If (-not(Test-Path "$strDestPath\kb0000001.msu"))
{
New-Item ("$strDestPath\kb0000001.msu")
New-Item ("$strDestPath\kb0000002.msu")
New-Item ("$strDestPath\kb0000003.msu")
New-Item ("$strDestPath\kb0000004.exe")
New-Item ("$strDestPath\kb0000005.exe")
New-Item ("$strDestPath\kb0000006.exe")
}
}
## Create INI's for files
Function CreateINI ($fileExtention, $SPNumber, $strDestPath)
{
Write-Host -ForegroundColor Magenta `n " ** Creating .INI files for any $fileExtention's that don't have one **" `n
## Obtain a list of KB*.extention files.
$arrList = get-childitem -path $strDestPath -name -filter "kb*.$fileExtention"
## If extention list is empty, abort
If ($arrList.Count -eq 0)
{
write-host -foregroundcolor "red" " No KB*.$fileExtention files found to work with."
}
Else {
## Start looping through the list stored in the array
Foreach ($strItem in $arrList) {
## Determine the patch INI name with path
Write-Host "Original file name with .exe: $strItem"
$INIFileName = ($strItem -replace ".$fileExtention",".ini")
$strDestINI = "$strDestPath\$INIFileName"
Write-Host "New INI file name with .ini : $INIFileName"
## If the destination patch INI already exists, skip processing to avoid overwriting.
If (Test-Path $strDestINI) {
write-host -ForegroundColor Red " $strDestINI exists. Skipping."
}
## Else, create a new patch INI from the template.
Else {
## Gets KB Number from file name
$KBNumber = ((($strItem -replace ".$fileExtention","").ToUpper()).TrimStart("KB"))
## Creates INI file
#OutputINIFile $fileExtention $KBNumber $SPNumber $strDestINI
## If File Extention is MSU, checks if file needs the wsusscan.cab file pulled and renamed
## Checks: Ignores file names with v. Example: KB1234567v2.msu
## Checks: Length of file name is greater than 13 chararacters. Example: KB1234567IE7.msu
If (($fileExtention = 'msu') -and `
($strItem -notlike '*v*') -and `
($strItem.Length -gt 13))
{
## Create the -scan.cab file
#Create-scanCab $KBNumber $strDestPath $strItem
}
}
}
}
}
Clear
## Main
## Variables in the script
$strDestPath = "C:\Temp\PatchBundle"
[Void][System.IO.Directory]::CreateDirectory($strDestPath)
$fileExtentions = "msu", "exe"
## Gets the service pack # based on the OS selected
$SPNumber = 2
CreateTestFiles $strDestPath
## Create .INI for all extentions files
Foreach ($extention in $fileExtentions)
{CreateINI $extention $SPNumber $strDestPath}
我花了3小時試圖找出答案。謝謝您的幫助。你搖滾! –