# do something with the file $file = Get-Item-Path$Path 'File {0} is of size {1} bytes.'-f$file.FullName, $file.Length }
Process-File-Path C:\windows\explorer.exe
结果看起来类似这样:
1 2 3 4 5
PS> Process-File-Path C:\windows\explorer.exe
File C:\windows\explorer.exe is of size 3903784 bytes.
PS>
这个函数每次只处理一个路径。如果希望传入多个路径,您需要这样做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
functionProcess-File { param ( [string[]] $Path )
foreach($SinglePathin$Path) { # do something with the file $file = Get-Item-Path$SinglePath 'File {0} is of size {1} bytes.'-f$file.FullName, $file.Length } }
process { foreach($SinglePathin$Path) { # do something with the file $file = Get-Item-Path$SinglePath 'File {0} is of size {1} bytes.'-f$file.FullName, $file.Length } } }
process { foreach($SinglePathin$Path) { # do something with the file $file = Get-Item-Path$SinglePath 'File {0} is of size {1} bytes.'-f$file.FullName, $file.Length } } }
# find all WMI classes... Get-WmiObject-Class * -List | # that contain the search keyword Where-Object { # is there a property or method with the keyword? $containsMember = ((@($_.Properties.Name) -like"*$Keyword*").Count -gt0) -or ((@($_.Methods.Name) -like"*$Keyword*").Count -gt0) # is the keyword in the class name, and is it an interesting type of class? $containsClassName = $_.Name -like"*$Keyword*"-and$_.Properties.Count -gt2-and$_.Name -notlike'Win32_Perf*' $containsMember-or$containsClassName } Write-Progress-Activity"Find WMI Classes"-Completed }
$classes = Find-WmiClass
$classes | # let the user select one of the found classes Out-GridView-Title"Select WMI Class"-OutputMode Single | ForEach-Object { # get all instances of the selected class Get-CimInstance-Class$_.Name | # show all properties Select-Object-Property * | Out-GridView-Title"Instances" }