powershell-2.0

What is the syntax to subscribe to an object's static event in PowerShell?

跟風遠走 提交于 2020-05-10 14:26:30
问题 Register-ObjectEvent looks for a object instance in the required parameter InputObject . What is the syntax for an object's static ( Shared ) event? UPDATE : Correct syntax for TimeChanged: $systemEvents = [Microsoft.Win32.SystemEvents] $timeChanged = Register-ObjectEvent -InputObject $systemEvents -EventName 'TimeChanged' -Action { Write-Host "Time changed" } Unfortunately, the SystemEvents will not be signaled in PowerShell ISE . Here's a sample using an object's staic event that works

What is the syntax to subscribe to an object's static event in PowerShell?

北慕城南 提交于 2020-05-10 14:25:52
问题 Register-ObjectEvent looks for a object instance in the required parameter InputObject . What is the syntax for an object's static ( Shared ) event? UPDATE : Correct syntax for TimeChanged: $systemEvents = [Microsoft.Win32.SystemEvents] $timeChanged = Register-ObjectEvent -InputObject $systemEvents -EventName 'TimeChanged' -Action { Write-Host "Time changed" } Unfortunately, the SystemEvents will not be signaled in PowerShell ISE . Here's a sample using an object's staic event that works

How to schedule a PS script in task scheduler which copies files from remote computer to local machine?

两盒软妹~` 提交于 2020-04-30 06:33:27
问题 My requirement was to copy certain files from remote machine to the local machine and I have written mapped the folder to the local machine.I use the drive letter of the mapped folder and the script works fine. But when I schedule the task, it throws error that the drive doesn't exists or isn't mapped. Please find the script below: $destination = "D:\Script\" $source = "T:\" $datefolders = Get-ChildItem $source $TodayString = (Get-Date).ToString("yyyy-MM-dd") foreach($folder in $datefolders)

Downloading jdk using powershell

萝らか妹 提交于 2020-02-20 04:06:35
问题 I am trying to download the java jdk using powershell scripting as given in the link below http://poshcode.org/4224 . Here as the author has specified , if I Change the source url where the latest jdk is present i.e., http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-windows-x64.exe the content is not getting loaded , only about 6KB gets downloaded . I have a doubt , whether the download limit in powershell script is only 6KB? Here is the code : $source = "http://download.oracle.com

Downloading jdk using powershell

旧时模样 提交于 2020-02-20 04:03:21
问题 I am trying to download the java jdk using powershell scripting as given in the link below http://poshcode.org/4224 . Here as the author has specified , if I Change the source url where the latest jdk is present i.e., http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-windows-x64.exe the content is not getting loaded , only about 6KB gets downloaded . I have a doubt , whether the download limit in powershell script is only 6KB? Here is the code : $source = "http://download.oracle.com

Recursively copy a set of files from one directory to another in PowerShell

折月煮酒 提交于 2020-02-18 05:52:20
问题 I'm trying to copy all *.csproj.user files recursively from C:\Code\Trunk to C:\Code\F2 . For example: C:\Code\Trunk\SomeProject\Blah\Blah.csproj.user Would get copied to: C:\Code\F2\SomeProject\Blah\Blah.csproj.user My current attempt is: Copy-Item C:\Code\Trunk -Filter *.csproj.user -Destination C:\Code\F2 -Recurse -WhatIf However I get: What if: Performing operation "Copy Directory" on Target "Item: C:\Code\Trunk Destination: C:\Code\F2\Trunk". First, it wants to put them all in a new

Recursively copy a set of files from one directory to another in PowerShell

断了今生、忘了曾经 提交于 2020-02-18 05:46:00
问题 I'm trying to copy all *.csproj.user files recursively from C:\Code\Trunk to C:\Code\F2 . For example: C:\Code\Trunk\SomeProject\Blah\Blah.csproj.user Would get copied to: C:\Code\F2\SomeProject\Blah\Blah.csproj.user My current attempt is: Copy-Item C:\Code\Trunk -Filter *.csproj.user -Destination C:\Code\F2 -Recurse -WhatIf However I get: What if: Performing operation "Copy Directory" on Target "Item: C:\Code\Trunk Destination: C:\Code\F2\Trunk". First, it wants to put them all in a new

Using range operator with a step

别来无恙 提交于 2020-02-03 10:19:19
问题 The PowerShell range operator generates a list of values: >1..6 1 2 3 4 5 6 How can I generate a list of values with a specific step? For example, I need a list from 1 to 10 with step 2. 回答1: The range operator itself doesn't support skipping/stepping, but you could use Where-Object (or the Where() method if you're running version 4.0 or above) to filter out every second: PS C:\> (1..10).Where({$_ % 2 -eq 0}) 2 4 6 8 10 Version 2.0 and up: PS C:\> 1..10 |Where-Object {$_ % 2 -eq 0} 2 4 6 8 10

Using range operator with a step

陌路散爱 提交于 2020-02-03 10:12:25
问题 The PowerShell range operator generates a list of values: >1..6 1 2 3 4 5 6 How can I generate a list of values with a specific step? For example, I need a list from 1 to 10 with step 2. 回答1: The range operator itself doesn't support skipping/stepping, but you could use Where-Object (or the Where() method if you're running version 4.0 or above) to filter out every second: PS C:\> (1..10).Where({$_ % 2 -eq 0}) 2 4 6 8 10 Version 2.0 and up: PS C:\> 1..10 |Where-Object {$_ % 2 -eq 0} 2 4 6 8 10

How to open image.png using ps1 script?

扶醉桌前 提交于 2020-02-02 11:08:49
问题 Setting the below $usbDriveLetter variable to automatically find the USB drive letter, and using that variable to open an image on the USB doesn't work. It literally prints "G:\image.png" in the cmd. $usbDriveLetter = (gwmi win32_volume -f 'label=''USB_NAME_HERE''').Name; "$usbDriveLetter" + "image.png" But if I don't use a var and make "G:\" static in the PowerShell script, the image opens just fine. G:\image.png So what am I doing wrong here? How do we dynamically open images using ps1