powershell-2.0

Powershell - Reboot and Continue Script

半腔热情 提交于 2019-11-26 22:20:21
问题 I'm looking for a way to continue a Powershell script from where it left off after calling a reboot in the script. For example, I am building a DC via Powershell automation, and after renaming the PC to TESTDC01, need to reboot, but after the reboot, continue with the script to go on to dcpromo etc. Is this possible? Cheers! 回答1: There is a great article on TechNet from the Hey, Scripting Guy series that goes over a situation very similar to what you are describing: Renaming a computer and

Get full path of the files in PowerShell

眉间皱痕 提交于 2019-11-26 22:19:37
问题 I need to get all the files including the files present in the subfolders that belong to a particular type. I am doing something like this, using Get-ChildItem: Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"} However, it's only returning me the files names and not the entire path. 回答1: Add | select FullName to the end of your line above. If you need to actually do something with that afterwards, you might have to pipe it into a foreach loop, like so: get

PowerShell 2.0 ConvertFrom-Json and ConvertTo-Json implementation

我的梦境 提交于 2019-11-26 20:37:49
I would like to monkeypatch a PowerShell 2.0 environment where the upgrade to 3.0 is not possible at this time. I am looking for a PowerShell 2.0 script implementation of the ConvertFrom-Json cmdlet and ConvertTo-Json cmdlet that are in PowerShell 3.0. I am most interested in the ConvertFrom-Json , but ConvertTo-Json would also be nice. Edward function ConvertTo-Json20([object] $item){ add-type -assembly system.web.extensions $ps_js=new-object system.web.script.serialization.javascriptSerializer return $ps_js.Serialize($item) } function ConvertFrom-Json20([object] $item){ add-type -assembly

How to get disk capacity and free space of remote computer

烂漫一生 提交于 2019-11-26 19:37:39
I have this one-liner: get-WmiObject win32_logicaldisk -Computername remotecomputer and the output is this: DeviceID : A: DriveType : 2 ProviderName : FreeSpace : Size : VolumeName : DeviceID : C: DriveType : 3 ProviderName : FreeSpace : 20116508672 Size : 42842714112 VolumeName : DeviceID : D: DriveType : 5 ProviderName : FreeSpace : Size : VolumeName : How do I get Freespace and Size of DeviceID C: ? I need to extract just these two values with no other informations. I have tried it with Select cmdlet, but with no effect. Edit: I need to extract the numerical values only and store them in

Equivalent to C#'s “using” keyword in powershell?

混江龙づ霸主 提交于 2019-11-26 19:13:43
问题 When I use another object in the .net-Framework in C# I can save a lot of typing by using the using directive. using FooCompany.Bar.Qux.Assembly.With.Ridiculous.Long.Namespace.I.Really.Mean.It; ... var blurb = new Thingamabob(); ... So is there a way in Powershell to do something similiar? I'm accessing a lot of .net objects and am not happy of having to type $blurb = new-object FooCompany.Bar.Qux.Assembly.With.Ridiculous.Long.Namespace.I.Really.Mean.It.Thingamabob; all the time. 回答1:

Executing an EXE file using a PowerShell script

和自甴很熟 提交于 2019-11-26 19:04:11
问题 I'm trying to execute an EXE file using a PowerShell script. If I use the command line it works without a problem (first I supply the name of the executable and series of parameters to invoke it): "C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe" C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode But doing the exact same thing inside of a script returns an error: The term '"C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe" C:\temp\TestProject1

PowerShell - How to Import-Module in a Runspace

≯℡__Kan透↙ 提交于 2019-11-26 18:19:15
问题 I am trying to create a cmdlet in C#. The code looks something like this: [Cmdlet(VerbsCommon.Get, "HeapSummary")] public class Get_HeapSummary : Cmdlet { protected override void ProcessRecord() { RunspaceConfiguration config = RunspaceConfiguration.Create(); Runspace myRs = RunspaceFactory.CreateRunspace(config); myRs.Open(); RunspaceInvoke scriptInvoker = new RunspaceInvoke(myRs); scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); Pipeline pipeline = myRs.CreatePipeline(); pipeline

How do I include a locally defined function when using PowerShell's Invoke-Command for remoting?

一曲冷凌霜 提交于 2019-11-26 17:39:23
问题 I feel like I'm missing something that should be obvious, but I just can't figure out how to do this. I have a ps1 script that has a function defined in it. It calls the function and then tries using it remotely: function foo { Param([string]$x) Write-Output $x } foo "Hi!" Invoke-Command -ScriptBlock { foo "Bye!" } -ComputerName someserver.example.com -Credential someuser@example.com This short example script prints "Hi!" and then crashes saying "The term 'foo' is not recognized as the name

How to perform keystroke inside powershell?

家住魔仙堡 提交于 2019-11-26 17:38:09
I have ps1 script to grab some information from the vmware cluster environment. In some place of ps1 script requires the ENTER button keystroke. So, How to do that ? -Thanks Adi Inbar If I understand correctly, you want PowerShell to send the ENTER keystroke to some interactive application? $wshell = New-Object -ComObject wscript.shell; $wshell.AppActivate('title of the application window') Sleep 1 $wshell.SendKeys('~') If that interactive application is a PowerShell script, just use whatever is in the title bar of the PowerShell window as the argument to AppActivate (by default, the path to

How to keep the shell window open after running a PowerShell script?

夙愿已清 提交于 2019-11-26 17:37:37
问题 I have a very short PowerShell script that connects to a server and imports the AD module. I'd like to run the script simply by double clicking, but I'm afraid the window immediately closes after the last line. How can I sort this out? 回答1: You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post. One-time Fix: Run your script from the PowerShell Console, or launch the PowerShell process using the -NoExit switch. e.g.