Chapters/emitting-objects-as-output/TMTools-Prelim/TMTools-Prelim.psm1
function Set-TMServiceLogon { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True)] [string]$ServiceName, [Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)] [string[]]$ComputerName, [Parameter(ValueFromPipelineByPropertyName=$True)] [string]$NewPassword, [Parameter(ValueFromPipelineByPropertyName=$True)] [string]$NewUser, [string]$ErrorLogFilePath ) BEGIN{} PROCESS{ ForEach ($computer in $ComputerName) { $option = New-CimSessionOption -Protocol Wsman $session = New-CimSession -SessionOption $option ` -ComputerName $Computer If ($PSBoundParameters.ContainsKey('NewUser')) { $args = @{'StartName'=$NewUser 'StartPassword'=$NewPassword} } Else { $args = @{'StartPassword'=$NewPassword} } $params = @{'CimSession'=$session 'MethodName'='Change' 'Query'="SELECT * FROM Win32_Service WHERE Name = '$ServiceName'" 'Arguments'=$args} $ret = Invoke-CimMethod @params switch ($ret.ReturnValue) { 0 { $status = "Success" } 22 { $status = "Invalid Account" } Default { $status = "Failed: $($ret.ReturnValue)" } } $props = @{'ComputerName'=$computer 'Status'=$status} $obj = New-Object -TypeName PSObject -Property $props Write-Output $obj $session | Remove-CimSession } #foreach } #PROCESS END{} } #function |