.hellu文件的powershell变量更新,但.exe文件不是

时间:2016-12-05 17:32:48

标签: powershell

我在文件夹中有.msu和.exe文件。我为每个人创建了INI文件。对于.msu文件,它运行得很好。对于.exe文件,它处理第一个,然后跳过其余的.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}

1 个答案:

答案 0 :(得分:1)

在第55行中,如果条件为msu文件,则将扩展名分配为msu(对于msu文件,这不会生效,但因为第二个exe文件扩展名替换不会发生),如下所示:

   If (($fileExtention = 'msu') -and `

我认为你的意思是

   If (($fileExtention -eq 'msu') -and `