powershell pass array to another function - windows

I have a function that gets all the processes accessing a particular folder and split it's process id. In this function, all the ids are saved in an array. I want to give this array to another function. The code i have is:
$CLRJson = Get-Content -Raw -Path "C:\Users\Lokal-keeran\Documents\Ausbildung\Aufgaben\Powershell\PI-Kill\PIKill.json" | ConvertFrom-Json
##Directory path to be scanned to see if there are processes accessing the files in the given directory.
$CLRDIRProd = $CLRJson.CLRExtproc_PARA_PROD.CLRDIR
$PIDGF = #()
#Get all Processes, accessing a folder
function Get-FileHandle ($HPath){
$handle = handle.exe $HPath
[System.Collections.ArrayList]$Process = $handle | Select-String -Pattern '.*?(?= +type:)' | Select-String -Pattern 'mstsc.exe' -NotMatch
Foreach ($pro in $Process.Matches.Value) {
$AddtoArray = $pro -split(":") | Select-Object -Last 1
$PIDGF += $AddtoArray
}
}
#Stop the Processes accessing that folder
function stop-FileHandle ([string[]]$HPathtoGFH) {
Get-FileHandle -HPath $CLRDIRProd
"The Third Value of this Array is: $PIDGF[2]"
#Stop Command......
}
stop-FileHandle -HPathtoGFH $CLRDIRProd
If i revoke stop-filehandle no output will be shown. Also the global array doesn't contain any value. But if i run the commands in the function Get-Filehandle, it shows me some process ids one below the another, just like it should be.....
Why array could not be given to another function ?

Inside function Get-FileHandle, stuff is added to a function local variable called $PIDGF.
Since you have declared your array outside the function, you should use script-scoping inside the function:
$script:PIDGF += $AddtoArray
Then in the stop-FileHandle function you need to use a subexpression ($())around the value you want to output:
"The Third Value of this Array is: $($script:PIDGF[2])"
Much better would be if the Get-FileHandle simply outputs the array so the calling function can capture that in a variable:
function Get-FileHandle ($HPath){
$handle = handle.exe $HPath
[System.Collections.ArrayList]$Process = $handle |
Select-String -Pattern '.*?(?= +type:)' |
Select-String -Pattern 'mstsc.exe' -NotMatch
foreach ($pro in $Process.Matches.Value) {
# just output the values, so the calling function receives an array
$pro -split(":") | Select-Object -Last 1
}
}
#Stop the Processes accessing that folder
function Stop-FileHandle ([string[]]$HPathtoGFH) {
$PIDGF = Get-FileHandle -HPath $CLRDIRProd
"The Third Value of this Array is: $($PIDGF[2])"
#Stop Command......
}
Stop-FileHandle -HPathtoGFH $CLRDIRProd
This way you do not use += concatenation to an existing array which is both time and memory consuming, plus you do not have to worry about scoping.

Related

How select-object cmdlet accepts input by pipeline?

Sorry for asking a novice question, as I am learning Powershell and I am confused how select-object -property parameter works with pipeline. As mentioned in help it doesnot accept the value through pipeline.
For e.g: the below code should have given an error:
get-process|select-object -property name,vm,pm
Can someone explain or guide me, thanks in advance.
To better understand how Select-Object works, here is a very simplified demo function that works similar to Select-Object:
function Select-ObjectSimplified {
[CmdletBinding()]
param (
# "ValueFromPipeline" means this parameter accepts pipeline input
[Parameter(Mandatory, ValueFromPipeline)] [PSObject] $InputObject,
# This parameter does NOT accept pipeline input
[Parameter(Mandatory)] [Object[]] $Property
)
# The process section runs for each object passed through the pipeline.
process {
# Create an ordered hashtable that will store the names and values
# of the selected properties.
$OutputObject = [ordered] #{}
# Loop over each property of $InputObject
foreach( $InputObjectProperty in $InputObject.PSObject.Properties ) {
# Check if the current property is listed in -Property argument.
if( $Property -contains $InputObjectProperty.Name ) {
# Add the current property to the output object.
$OutputObject[ $InputObjectProperty.Name ] = $InputObjectProperty.Value
}
}
# Convert the hashtable to a PSCustomObject and (implicitly) output it.
[PSCustomObject] $OutputObject
}
}
Demo:
# Create an array of two objects.
$example = [PSCustomObject]#{ Foo = 4; Bar = 8 },
[PSCustomObject]#{ Foo = 15; Bar = 16 }
# Pass the array as input to the pipeline.
$example | Select-ObjectSimplified -Property Foo | Format-List
Output:
Foo : 4
Foo : 15
Although the parameter -Property doesn't accept pipeline input, we can still use it when we process the pipeline input that binds to parameter -InputObject. There is no need for -Property to accept pipeline input, because it stays constant during the whole run of the pipeline.
The demo is executed by PowerShell like this:
$example | Select-ObjectSimplified -Property Foo | Format-List
The argument "Foo" gets bound to parameter -Property. The parameter -InputObject is not bound yet, because we didn't explicitly pass an argument to it.
The first element of the array $example is passed through the pipeline. The argument [PSCustomObject]#{ Foo = 4; Bar = 8 } gets bound to parameter $InputObject.
The process{} section runs. There we can get the current pipeline object from $InputObject and get the argument of parameter -Property from $Property. So $InputObject will be different for each run of process{}, but $Property does not change, it is constant.
The second element of the array $example is passed through the pipeline. The argument [PSCustomObject]#{ Foo = 15; Bar = 16 } gets bound to parameter $InputObject.
Like 3), but with different value for $InputObject.
Hope that shed some light on the topic. To get an even better understanding I suggest to read About Pipelines and then follow a tutorial to write your own pipeline function. The concept only did really click for me, once I successfully wrote my first real pipeline functions.

Powershell problem with service status searching

Hi guys its maybe a easy question for you but im newbie from powershell so can you pls help me?
In school I got an assignment where I needed to make a menu, a script that could search from service to status, and a script that could search from status to service, and it looks like this:
elseif ($menu -eq "2") {
$statusbank = (Get-Service).Status
$sstatuss = Read-Host "Bitte geben Sie ein Status ein z.B Running/Stopped"
if ($statusbank.Contains([string]$sstatuss)) {
$Information = (Get-Service | Where-Object {$_status -eq $sstatuss}).Name | format-list -property Name
Write-Host $Information
}
}
i really dont understand where my problem is.
It dosn't work: It doesn't do anything and then just ends the script
If i debug, i only see it will skip this, even they are a lot of true value in $statusbank :
if ($statusbank.Contains([string]$sstatuss)) {
Try using this instead:
elseif ($menu -eq "2")
{
$statusbank = Get-Service
$sstatuss = Read-Host "Bitte geben Sie ein Status ein z.B Running/Stopped"
if($sstatuss -match '^(Running|Stopped)$' -and $sstatuss -in $statusbank.Status)
{
$statusbank | Where-Object Status -EQ $sstatuss |
Format-Table -Property Name,Status
}
}
To complement Santiago Squarzon's helpful answer with an optimization:
# Prompt until a valid service status identifier is entered.
do {
try {
[System.ServiceProcess.ServiceControllerStatus] $sStatus =
Read-Host "Please specify the desired service status (e.g., Running or Stopped)"
break # A valid value was entered, exit the loop
} catch { }
Write-Warning "Unknown status; please specify one of: $([Enum]::GetNames([System.ServiceProcess.ServiceControllerStatus]))"
} while ($true)
# Now output the names of all services that are in the specified state, if any:
(Get-Service | Where-Object Status -eq $sStatus).Name
Casting the user input (which is always a string) to type [System.ServiceProcess.ServiceControllerStatus] (the type of the .Status property of the objects returned by Get-Service) is used to ensure that a valid service-status identifier was entered.
As for what you tried:
Leaving the inefficiency of calling Get-Service twice aside, your primary problem was the use of the .Contains() .NET array method (implemented via the IList interface):
.Contains() performs no on-demand type conversions, so looking for a string ($sstatuss) in your array of [System.ServiceProcess.ServiceControllerStatus] values ($statusbank) never succeeds.
By contrast, PowerShell's -contains operator does perform on-demand type conversions (as PowerShell generally does) and is notably also case-insensitive (as PowerShell generally is). The same applies to functionally equivalent, but operands-reversed -in operator.
To illustrate the difference:
# Sample array with [System.ServiceProcess.ServiceControllerStatus] elements.
$array = [System.ServiceProcess.ServiceControllerStatus]::Running,
[System.ServiceProcess.ServiceControllerStatus]::Stopped
# WRONG.
$array.Contains('running') # !! always $false with a [string] as input
# OK.
$array -contains 'running' # -> $true - on-demand type conversion
# from string to [System.ServiceProcess.ServiceControllerStatus]
In a nutshell: -contains is in effect using the -eq operator against each element behind the scenes, so the latter's automatic type conversions and case-insensitivity apply. See the bottom section of this answer for more information about -contains and -in.
Pitfall: Due to having the same name, there's potential for confusion with the .Contains() string method, which functions differently, however: it performs literal substring matching, and there is no direct operator equivalent in PowerShell for that - see this answer.
Also:
Format-* cmdlets output objects whose sole purpose is to provide formatting instructions to PowerShell's output-formatting system - see this answer. In short: only ever use Format-* cmdlets to format data for display, never for subsequent programmatic processing.
Write-Host is typically the wrong tool to use, unless the intent is to write to the display only, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, or redirect it to a file. To output a value, use it by itself; e.g., $value instead of Write-Host $value (or use Write-Output $value, though that is rarely needed); see this answer

Piping Powershell multiple functions together

I have this going on
Function A($data){
#Function accepts $data which is Get-Content of a file
#Function does some stuff and then return
#Function return data as String
}
Function B($data){
#This Function takes data given from Function A, manipulate it and return custom object
}
$Function C ($data1, $data2){
#This function Takes 2 custom objects created from Function B and prints out some data
}
#For this is to work i need to do this for example:
$file = 'c:\test.txt'
$data1 = A (Get-Content $file1)
$data1 = B ($data1)
#Same thing for data2 and then use function C:
C -data1 $data2 -data2 data2
though this works, I would like to use Piping, i must be using it wrong
Get-Content $file1 | A | B
Would give me errors obviously.
can someone help me pipe this?
Continued from my comment. For Example:
Building PowerShell Functions That Support the Pipeline
ValueFromPipeline Let's start off with a function to perform some
tests.
#region Test Function
Function Test-Object
{
[cmdletbinding()]
Param
(
[parameter(ValueFromPipeline)][int[]]$Integer
)
Process
{
$PSItem
}
}
#endregion Test Function

How does one Transform a Collection of Objects into a Collection of new Objects of another Type in Powershell?

Supposing I have a method that gets a collection of enums of my enum type ProjectName and a have a collection of Server objects I want to associate with those ProjectNames in a type called Project; how would I do this in a Powershell Select-Object (or some other equivalent of LINQ's Select).
The C# equivalent of what I want to produce in Powershell is this:
var servers = new[]
{
new Server(/*someOtherProjectsCollection goes here*/),
new Server(/*someOtherProjectsCollection goes here*/),
new Server(/*someOtherProjectsCollection goes here*/)
};
var projects = GetProjectNames().Select(projectName => new Project(projectName, servers.Where(server => server.Projects.Any(serverProject => serverProject.Name == projectName))));
But what I have is this:
$servers = [Server]::new(/*someOtherProjectsCollection goes here*/), [Server]::new(/*someOtherProjectsCollection goes here*/), [Server]::new(/*someOtherProjectsCollection goes here*/)
$projects = (GetProjectNames()) | Select-Object {
$selectedProjectName = $_
return [Project]::new($_, ($servers | Where-Object { $_.projects.Where({ $_ -eq $selectedProjectName }).Count -gt 0 }))
}
When I try and read $projects back in Powershell LSE (whilst on a breakpoint after this last line), it just returns the code as a string and I can't even cast it to [Project[]]. I think the problem might be with the use of curly braces with Select-Object but I'm not sure how else to create a new Project object within the Select-Object.
You want ForEach-Object instead of Select-Object to return a new [Project] instance for each project name; also, your code can be streamlined:
$projects = GetProjectNames | ForEach-Object {
$projectName = $_
[Project]::new(
$projectName,
$servers.Where({ $_.projects -eq $projectName })
)
}
Select-Object is for creating new custom objects based on select properties from the input objects; by contrast, you're constructing a new, specific type instance from each input object, which must be done in a ForEach-Object call, where you explicitly control the output.
$_.projects -eq $projectName as a conditional relies on PowerShell's ability to use -eq with an array as the LHS, in which case filtering is performed, and a filtered subarray is returned; since .Where() interprets the script block's output as a Boolean, an empty subarray is interpreted as $false, whereas one with at least one element is interpreted as $true.
Also note that you don't need an explicit return, given PowerShell's implicit output behavior: since the newly constructed [Project] instance isn't assigned to a variable or sent elsewhere, it is automatically returned.

Correct conditional logic for multiple flags

I have a file that resembles the following
C:\path\AP1\com1\web.config
C:\path\AP2\com1\web.config
C:\path\AP1\com2\web.config
C:\path\AP2\com2\web.config
C:\path\AP1\com4new\web.config
C:\path\AP2\com4new\web.config
C:\path\AP1\thirdFolder\web.config
C:\path\AP2\thirdFolder\web.config
This file gets output on every server regardless of environment. I am creating a validation script to only include the relevant lines for each server.
An example of what the server data looks like is this
<server compName="serverName102">
<ip>192.168.1.1</ip>
<type>App</type>
<env>test</env>
<instances>Com2, Com3</instances>
<AppPools>bo, fo</AppPools>
</server>
If a server's AppPools setting contains fo, then I need to include any lines that contain AP1. If AppPools contains bo, then I need AP2, and obviously if it contains both I need both lines.
If a server's instances contains Com2, then I need to include any lines that contain com2 OR com4new. Com3 will be the thirdFolder lines.
I currently have several if statements that look like the following
if ($serverInst -like "*com2*"){
$refinedResults += $allResults | where-object {$_ -like "*com2*" -or $_ -like "*com4new*"}
}
The problem is that I am almost positive that how I am doing this is not nearly the best way, or how I should be doing this. Can anybody provide some insight to the best way to perform this validation?
The major problem I see with your approach is the fact that you might end up with duplicates in your $refinedResults collection.
To work around this, you can employ a HashSet of strings.
If you try to add a value to a HashSet and it already exists, the operation simply returns false and nothing happens to the Set.
Assuming that $allResults is an array of strings, you could do this:
$refinedResults = New-Object System.Collections.Generic.HashSet[String]
if ($serverInst -like "*com2*")
{
$refinedResults.UnionWith([String[]]($allResults |? {$_ -like "*com2*" -or $_ -like "*com4new*"}))
}
For readability (and performance if you plan on processing more than one entry at a time), you might want to define your rules and extract from the $allResults before you start matching:
[String[]]$resultsCom2 = $allResults |? {$_ -like "*com2*" -or $_ -like "*com4new*"}
[String[]]$resultsCom3 = $allResults |? {$_ -like "*thirdfolder*"}
$refinedResults = New-Object System.Collections.Generic.HashSet[String]
if ($serverInst -like "*com2*")
{
$refinedResults.UnionWith($resultsCom2)
}
if ($serverInst -like "*com3*")
{
$refinedResults.UnionWith($resultsCom3)
}
If the instantiation of $refinedResults fails, add:
Add-Type -AssemblyName System.Core
to your script before it

Resources