cmdlets

Using Powershell's Invoke-Command to call a batch file with arguments

别说谁变了你拦得住时间么 提交于 2019-12-01 03:41:27
I want to use Powershell in order to call a batch file on remote machines. This batch file has arguments. Here's what I have so far: $script = "\\fileshare\script.cmd" $server = $args[0] $args [string]::join(',',$args[1 .. ($args.count-1)]) Invoke-Command -computername $server {$script + ' ' + $args} After a bit of searching, I found that the Invoke-Command function runs its scriptblock in a whole new process, so you can't put variables in it (they won't get expanded). That's what the -ArgumentList tag is for. So I tried this instead... Invoke-Command -computername $server {\\fileshare\script

Reading event log remotely with Get-EventLog in Powershell

偶尔善良 提交于 2019-12-01 03:19:31
I've a powershell script which runs on server(test-server) and reads the log file of his client(DC1). Both sides can ping to each other. On both sides, firewalls are disabled. Remote Desktop and Remote Assistance are enabled on DC1. Get-EventLog System -ComputerName test-server -Source Microsoft-Windows-Winlogon # WORKS Get-EventLog System -ComputerName DC1 -Source Microsoft-Windows-Winlogon # DOESN'T WORK I run this script on test-server. As you see when I read the local log file on test-server it works fine but if I try to read the log file of DC1 remotely I get the error " Get-EventLog :

How do I add a column of incrementing values to cmdlet output?

南笙酒味 提交于 2019-11-30 18:20:22
问题 Suppose I call Get-Service and want to assign a new column ID with the cmdlet output that prints incrementing integers so that: ID Status Name DisplayName -- ------ ---- ----------- 0 Running AdobeARMservice Adobe Acrobat Update Service 1 Stopped AeLookupSvc Application Experience 2 Stopped ALG Application Layer Gateway Service I'm trying to use Select-Object right now to add this column, but I don't quite understand how to iterate a variable in this sort of expression. Here's what I've got:

Objects with no '.Count' Property - use of @() (array subexpression operator) vs. [Array] cast

送分小仙女□ 提交于 2019-11-29 07:21:12
I am trying to perform some simple if statements, but all of the newer cmdlets that are based upon [Microsoft.Management.Infrastructure.CimInstance] don't seem to expose a .count method? $Disks = Get-Disk $Disks.Count Doesn't return anything. I found that I can cast this as an [array], which makes it returns a .NET .count method as expected. [Array]$Disks = Get-Disk $Disks.Count This works without directly casting it as an array for previous cmdlets: (Get-Services).Count What is the recommended way to get around this? An example that doesn't work: $PageDisk = Get-Disk | Where {($_.IsBoot -eq

Can we see the source code for PowerShell cmdlets?

耗尽温柔 提交于 2019-11-28 20:12:56
I'm learning some PowerShell. Is it possible to see the source code for a built-in cmdlet like Get-ChildItem ? halr9000 Actually, your best bet is to go check out PowerShell Community Extensions . This open source software community project is "aimed at providing a widely useful set of additional cmdlets...". The developers on the project are PowerShell MVPs and know their stuff. As far as using reflection on the existing PowerShell cmdlets, PowerShell MVP Oisin Grehan made a handy function titled " Reflect-Cmdlet ". I won't steal his code and place it here, but basically what you do is: Get

How do you support PowerShell's -WhatIf & -Confirm parameters in a Cmdlet that calls other Cmdlets?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 19:22:46
I have a PowerShell script cmdlet that supports the -WhatIf & -Confirm parameters. It does this by calling the $PSCmdlet.ShouldProcess() method before performing the change. This works as expected. The problem I have is that my Cmdlet is implemented by calling other Cmdlets and the -WhatIf or -Confirm parameters are not passed along to the Cmdlets I invoke. How can I pass along the values of -WhatIf and -Confirm to the Cmdlets I call from my Cmdlet? For example, if my Cmdlet is Stop-CompanyXyzServices and it uses Stop-Service to implement its action. If -WhatIf is passed to Stop

How to properly use the -verbose and -debug parameters in custom cmdlet

南笙酒味 提交于 2019-11-28 17:27:11
By default, any named function that has the [CmdletBinding()] attribute accepts -debug and -verbose (and a few others) parameters and has predefined $debug and $verbose variables. What I'm trying to figure out is how to pass them on to other cmdlet's that that get called within the function. Let's say I have a cmdlet like this: function DoStuff() { [CmdletBinding()] PROCESS { new-item Test -type Directory } } if -debug or -verbose was passed into my function I want to pass that flag into the new-item cmdlet. What's the right pattern for doing this? Roman Kuzmin Perhaps it sounds strange but

Powershell function dispose or abort handler

旧巷老猫 提交于 2019-11-28 09:14:54
问题 I have a pipe function that allocates some resources in begin block that need to be disposed at the end. I've tried doing it in the end block but it's not called when function execution is aborted for example by ctrl + c . How would I modify following code to ensure that $sw is always disposed: function Out-UnixFile([string] $Path, [switch] $Append) { <# .SYNOPSIS Sends output to a file encoded with UTF-8 without BOM with Unix line endings. #> begin { $encoding = new-object System.Text

How do I host a Powershell script or app so it's accessible via WSManConnectionInfo? (like Office 365)

对着背影说爱祢 提交于 2019-11-28 06:53:59
The only ways I know to connect to a remote runspace include the following parameters WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "localhost", 80, "/Powershell", "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential); or WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "localhost", 5985, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.Powershell", credential); How do I set up my own custom Powershell object so I can expose it through HTTP? What are the correct parameters to use and how do I set them up? There are

How to call PowerShell cmdlets from C# in Visual Studio

别等时光非礼了梦想. 提交于 2019-11-28 05:05:36
问题 I'm creating a PowerShell cmdlets from Visual Studio and I can't find out how to call cmdlets from within my C# file, or if this is even possible? I have no trouble running my cmdlets one by one, but I want to set up a cmdlet to run multiple cmdlets in a sequel. 回答1: Yes, you can call cmdlets from your C# code. You'll need these two namespaces: using System.Management.Automation; using System.Management.Automation.Runspaces; Open a runspace: Runspace runSpace = RunspaceFactory.CreateRunspace(