-PipelineVariable isnt working as intended - powershell

I have this script that changes services per a csv file input
Import-CSV .\SSAS_services.csv |
ForEach-Object{
Get-Service $_.Service -ComputerName $_.Server -PipelineVariable svc|
Set-Service -Status $_.Task -StartupType $_.'Startup Type' -PassThru
} |
Select-Object MachineName, Name, Status, StartType, #{n='OldStatus';e={$svc.Status}}, #{n='OldStartType';e={$svc.StartType}} |
tee-object -FilePath '.\ChangeServices_LOG.txt' #-Append
Server,Service,Startup Type,Task
DCVPIM108,SQL Server Analysis Services (MSSQLSERVER),automatic,start
server2,"SQL Server Analysis Services (MSSQLSERVER), SQL Server Analysis Services (MSSQLSERVER) CEIP",Manual,stop
it works great, except for my -PipelineVariable svcis not working as intended. if a service was "stopped" and "Manual" before being changed to "running" and "automatic", it doesnt get the old values "stopped" and "Manual" for OldStatus and OldStartType
MachineName : DCVPIM108
Name : MSSQLServerOLAPService
Status : Running
StartType : Automatic
OldStatus : Running
OldStartType : Automatic
why is that?

The -PipelineVariable / -pv common parameter only works:
within a single pipeline.
in script blocks in later segments of the same pipeline.
Since you're using it in a pipeline that is nested inside the ForEach-Object script block, the commands in the outer pipeline cannot use it.
However, I suggest restructuring your command so that you don't need a pipeline variable for Get-Service anymore.
Instead,
-PipelineVariable $csvRow is used with Import-Csv, so that you can more easily refer to it even in nested pipelines (the alternative would be to define the variable explicitly at the start of the ForEach-Object script block as $csvRow = $_).
$svc is then declared as an -OutVariable, so that the original service state is captured before Set-Service is called to change it.
Getting a service, setting its startup type, and enriching the CSV-row object with additional information now all happen inside the ForEach-Object script block.
Import-CSV .\SSAS_services.csv -PipelineVariable csvRow | ForEach-Object {
Get-Service -Name $csvRow.Service -ComputerName $csvRow.Server -OutVariable svc |
Set-Service -Status $csvRow.Task -StartupType $csvRow.'Startup Type'
$csvRow | Select-Object MachineName, Name, Status, StartType,
#{n='OldStatus';e={$svc.Status}},
#{n='OldStartType';e={$svc.StartType}}
} | Tee-object -FilePath '.\ChangeServices_LOG.txt'

I guess what you want is to pass same object down the multiple pipes. I haven't use -PipeLineVariable much, but looks like it just creating a nicer alias for $_ . If you need to push something specific down the pipeline I guess you need to use write-ouput with custom object or hashtable. Below is a dummy sample, pushing down and modifying a hastable:
$services = "xagt" , "xbgm" , "XblGameSave"
$list = new-object System.Collections.ArrayList
$serv | foreach {
$svc = Get-Service $_ ; Write-Output #{Name = $svc.Name; Stat=$svc.Status}
} | foreach {$_.SomeNewItem = "new stuff"; $list.Add($_)}
But in your case one pipeline might be sufficient. Try something like that:
Import-CSV .\SSAS_services.csv | foreach {
$old = Get-Service $_.Service;
Set-Service -Name $_.Service -Status Running
$new = Get-Service $_.Service;
$data = $_.MachineName, $_.Service, $old.Status, $new.Status -join ","
Write-Host $data
$data >> Log.txt
}

Related

Deactivate running/ready Tasks and activate them again

I need a Powershell script, that deactivates a bunch of running/ready Tasks on multiple machines, so that we can update them. After we updated the machines i should be able to trigger the next part of the script, that activates specifically the tasks that the first part of the script deactivated.
Automatation and the Update part should not be part of the script, no timed actions needed or anything fancy.
I trigger Part 1 -> Script deactivates Tasks
I trigger Part 2 -> Script activates Tasks the Part 1 deactivated
For Part 1 i got this so far:
Invoke-Command -ComputerName xxx-11, xxx-12, xxx-14 -ScriptBlock {Get-ScheduledTask -TaskPath \yyy\ | Where {($_.State -like "running") -or ($_.State -like "ready")} | Disable-ScheduledTask }
I am unable to create a logic, that remembers what tasks were deactivated, so that the Part 2 can activate them again instead of simply activating EVERY task.
I guess what you want is to collect an array of objects storing the computername, the taskpath and the task name for each of the disabled tasks.
Try:
$result = Invoke-Command -ComputerName 'xxx-11', 'xxx-12', 'xxx-14' -ScriptBlock {
Get-ScheduledTask -TaskPath '\yyy\' | Where-Object {'running','ready' -contains $_.State} | ForEach-Object {
$null = $_ | Disable-ScheduledTask
# output an object to be collected in $result
$_ | Select-Object #{Name = 'ComputerName'; Expression = {$env:COMPUTERNAME}}, TaskPath, TaskName
}
}
# remove the extra properties Invoke-Command added
$result = $result | Select-Object * -ExcludeProperty PS*, RunSpaceId
You now have the info you need to reactivate the tasks later in variable $result
If you like you can save this to CSV file for later reference:
$result | Export-Csv -Path 'X:\Somewhere\DisabledTasks.csv' -NoTypeInformation

Skype for Business Service Health for Multiple Devices

I've been working at this script for a while and I can't seem to figure it out:
$servers = Get-Content -path c:\users\jason\documents\skyperservers.txt
Foreach ($server in $servers){get-cswindowservice -computername $servers | where-object {$_.status -eq "running"}}
I keep getting the error
Get-CSWindowService : Cannot convert 'System.Object[]' to the type
'System.String' required by parameter 'ComputerName'. Specified method
is not supported...
Essentially, I'm trying to display all services from the command for each skype server and their service health status whether "running" or "stopped"
Two things about your code:
You are iterating the collection $servers using variable $server, but you do not use that as parameter for the Get-CSWindowService cmdlet. Instead you feed it the entire collection $servers (System.Object[]), which is what the error is telling you.
If you also want to see servers where the CsWindowService is stopped, add this to your Where-Object clause and best return objects so you can combine the service status with the server name.
Try
$servers = Get-Content -Path 'c:\users\jason\documents\skyperservers.txt'
$result = foreach ($server in $servers) {
Get-CsWindowService -ComputerName $server |
# or use regex: Where-Object {$_.Status -match 'Running|Stopped'}
Where-Object {$_.Status -eq 'Running' -or $_.Status -eq 'Stopped'} |
# include the server name in the output
Select-Object #{Name = 'ComputerName'; Expression = {$server}}, Status
}
Now you can display the results on screen
$result
or for instance save the results to a Csv file
$result | Export-Csv -Path 'X:\SkypeServers_Status.csv' -NoTypeInformation

Selecting value from first pipelined argument

I wrote a PowerShell script that gets some cluster information. One of the columns I need is from the first argument in the pipeline and I can't find a way to return it's value.
function Get-SQL-Clusters {
Param([string]$server)
$servers = Get-Content -LiteralPath "C:\temp\sql_clusters.txt"
if ($server -ne 1) {
$files = foreach ($box in $servers) {
Invoke-Command -ComputerName $box {
Get-ClusterResource | Get-ClusterParameter
} | Where-Object {
$_.Name -eq "Address"
} | Format-Table PSComputerName, ClusterObject, State, Name, Value -AutoSize
}
} else {
Write-Warning "'$server' is not a valid path."
}
return $files
}
When I run this, I get the data I need but State is blank. It's in Get-ClusterResource, but the IP, which is what I'm mostly looking for, is in Get-ClusterParameter.
Ideally I would like to return the name of the cluster, each of the alwayson names, it's IP and it's current state so I can see if the active IP is on the primary site or on the DR site.
Your call to Invoke-Command places the Get-ClusterResource | Get-ClusterParameter calls into its own script block {...}, then pipes the results of evaluating those expressions to the Where-Object cmd. This may not be the intended order of operations.
Project your results using the Select-Object cmdlet at intermediate places in your pipeline to give you access to the desired properties at later stages (specific syntax hasn't been checked;YMMV):
Invoke-Command -ComputerName $box { Get-ClusterResource | Select-Object -Property State, #{Name="ClusterParameter";Expression = {(Get-ClusterParameter -InputObject $_) }}| Where-Object { $_.ClusterParameter.Name -eq ...
Will produce objects like:
State | ClusterParameter
------------------------
foo ClusterParameter.ToString()
The almost final code. It's not 100% complete but I get the State and IP values now and will fix the rest later. Another change I made was to stop using text files and created a hash table for my servers because of formatting problems I had with text files.
function Get-SQL-Clusters-scrap
{
param([string]$server)
import-module c:\temp\sql_hashtable2.ps1
$servers = $sql_servers.hadr
if ($server -ne 1)
{
$files = ForEach ($box in $servers) {invoke-command -ComputerName $box {Get-ClusterResource |
foreach-object {($state) = ($_.State); $_ |
get-clusterparameter |Where-Object {$_.Name -eq "Address"} |
Format-Table ClusterObject,#{Name=”State”;Expression={$state}}, Name, Value, PSComputerName -AutoSize}}}
}
else
{Write-Warning "'$server' is not a valid path."}
return $files
}

Get-Service results to array

I'm sure this should be straight forward but I've been stuck on it for a while now...
I am trying to get the service names (for sql server) into an array but can't figure out how to do it. I basically want the array contents to look something like the output of this:
Get-Service -computername $server_name -name sql* | format-table -property name
I have tried things like this but the contents of $service_name are very odd:
$service_name = (Get-Service -computername $server_name -name sql* -exclude *sqlwriter | format-table -property name)
Whatever I try either errors or gives some strange message in my array. Should this be easy/possible? I'm guessing I could dump the results in a text file then use the contents of that but it's a bit messy and more overhead than seems necessary.
Not sure, but where you looking for this (with whatever variation on the pattern for -Name and/or -Exclude)?
PS> $names = (Get-Service -Name Sql* | select name)
PS> $names.GetType().IsArray
True
PS> $names.Length
3
PS> $names
Name
----
SQLBrowser
SQLSERVERAGENT
SQLWriter
Or even along the lines of the following if you really want the "Name" as a System.String array.
PS> $names = (Get-Service -Name Sql* | foreach { $_.Name -as [string]})
PS> $names[0].GetType().FullName
System.String
PS> $names
SQLBrowser
SQLSERVERAGENT
SQLWriter
But also keep the good advice in #alroc's answer in mind - maybe you want to keep the actual type of Get-Service's result (System.ServiceProcess.ServiceController) as long as possible and access/use the Name property of it as late as possible. YMMV.
You're using format-table in the pipeline. Anytime you use a format-* cmdlet, that's the end of the line for your data - it's now just a formatted bunch of text, it's no longer data that you can actually use.
Try this to get the names formatted as a table for viewing:
$service_name = (Get-Service -computername $server_name -name sql* -exclude *sqlwriter);
$service_name | format-table -property name;
That said, I ran both your version and mine above and got the same visual output - the critical difference is that mine stores data in $service_name, not just a bunch of text (as yours does).
edit:
In response to:
All I really needed was the service names in an array to then use elsewhere
$service_name = (Get-Service -computername $server_name -name sql* -exclude *sqlwriter) | select-object -expandproperty name;
or:
$service_name = (Get-Service -computername $server_name -name sql* -exclude *sqlwriter).name;
No need to invoke WMI directly.

Formatting the results of a powershell script

How can i add the server name at the left of each line result on this script?. Thank you!
$servers = Get-Content -path .\Machines.txt
[pscustomobject]$result = #()
$subresult =
ForEach ($server in $servers)
{
Set-Service -computername $servers -Name sacsvr -StartupType Disabled -PassThru
}
$result = $subresult
$result | Out-File local_group_members.csv
This is an example result:
Status Name DisplayName
------ ---- -----------
Stopped sacsvr Special Administration Console Helper
Stopped sacsvr Special Administration Console Helper
Stopped sacsvr Special Administration Console Helper
Alternatively you can just add a property to the objects you're outputting right now. Pipe your Set-Service to Add-Member like this:
Set-Service -computername $servers -Name sacsvr -StartupType Disabled -PassThru | Add-Member -MemberType NoteProperty -Name 'Server' -Value $Server -PassThru
Now each object that you pass to $subresult has a new property Server that is the name of the server it was run on. You'll probably want to pipe through Select when outputting to have the order you want.
$SubResult | Select Server, Status, Name, DisplayName | Export-CSV 'local_group_members.csv' -NoType
You can arbitrarily re-order or add to your output with Select-Object. You can use hash tables to include calculated properties such as your desired ServerName.
So for each server, you can set the services and tag the output with that server name:
ForEach ($server in $servers)
{
Set-Service -computername $server -Name sacsvr -StartupType Disabled -PassThru |
Select #{Name = 'ServerName'; Expression = {$server}}, Name, DisplayName, Status
}
The above is shorthand for:
Select-Object -Property (properties)
The -Property parameter allows you to select any arbitrary grouping of properties on the type of object being piped in. Another parameter, -InputObject allows us to pipe in objects by value.