How to properly -filter multiple strings in a PowerShell copy script

前端 未结 4 500
暗喜
暗喜 2020-11-29 00:01

I am using the PowerShell script from this answer to do a file copy. The problem arises when I want to include multiple file types using the filter.

Get-Chi         


        
相关标签:
4条回答
  • 2020-11-29 00:31

    use the include is the easiest way as per

    http://www.vistax64.com/powershell/168315-get-childitem-filter-files-multiple-extensions.html

    0 讨论(0)
  • 2020-11-29 00:41

    -Filter only accepts a single string. -Include accepts multiple values, but qualifies the -Path argument. The trick is to append \* to the end of the path, and then use -Include to select multiple extensions. BTW, quoting strings is unnecessary in cmdlet arguments unless they contain spaces or shell special characters.

    Get-ChildItem $originalPath\* -Include *.gif, *.jpg, *.xls*, *.doc*, *.pdf*, *.wav*, .ppt*
    

    Note that this will work regardless of whether $originalPath ends in a backslash, because multiple consecutive backslashes are interpreted as a single path separator. For example, try:

    Get-ChildItem C:\\\\\Windows
    
    0 讨论(0)
  • 2020-11-29 00:48

    Something like this should work (it did for me). The reason for wanting to use -Filter instead of -Include is that include takes a huge performance hit compared to -Filter.

    Below just loops each file type and multiple servers/workstations specified in separate files.

    ##  
    ##  This script will pull from a list of workstations in a text file and search for the specified string
    
    
    ## Change the file path below to where your list of target workstations reside
    ## Change the file path below to where your list of filetypes reside
    
    $filetypes = gc 'pathToListOffiletypes.txt'
    $servers = gc 'pathToListOfWorkstations.txt'
    
    ##Set the scope of the variable so it has visibility
    set-variable -Name searchString -Scope 0
    $searchString = 'whatYouAreSearchingFor'
    
    foreach ($server in $servers)
        {
    
        foreach ($filetype in $filetypes)
        {
    
        ## below creates the search path.  This could be further improved to exclude the windows directory
        $serverString = "\\"+$server+"\c$\Program Files"
    
    
        ## Display the server being queried
        write-host “Server:” $server "searching for " $filetype in $serverString
    
        Get-ChildItem -Path $serverString -Recurse -Filter $filetype |
        #-Include "*.xml","*.ps1","*.cnf","*.odf","*.conf","*.bat","*.cfg","*.ini","*.config","*.info","*.nfo","*.txt" |
        Select-String -pattern $searchstring | group path | select name | out-file f:\DataCentre\String_Results.txt
    
        $os = gwmi win32_operatingsystem -computer $server
        $sp = $os | % {$_.servicepackmajorversion}
        $a = $os | % {$_.caption}
    
        ##  Below will list again the server name as well as its OS and SP
        ##  Because the script may not be monitored, this helps confirm the machine has been successfully scanned
            write-host $server “has completed its " $filetype "scan:” “|” “OS:” $a “SP:” “|” $sp
    
    
        }
    
    }
    #end script
    
    0 讨论(0)
  • 2020-11-29 00:48
    Get-ChildItem $originalPath\* -Include @("*.gif", "*.jpg", "*.xls*", "*.doc*", "*.pdf*", "*.wav*", "*.ppt")
    
    0 讨论(0)
提交回复
热议问题