powershell

How to detect if there is any file added in a folder?

你说的曾经没有我的故事 提交于 2021-02-19 08:06:42
问题 Is it a way to detect if there is any file added in a folder? Include the sub-folder. For example, check if any text file *.txt is added in folder c:\data-files\ or its sub-folders. The folder can be shared folder of another machine too. 回答1: Perhaps you are confused on the types of events that are triggered: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher_events(v=vs.110).aspx This should work, taken from the link above and modified for your requirements: #By BigTeddy 05

How to detect if there is any file added in a folder?

此生再无相见时 提交于 2021-02-19 08:06:39
问题 Is it a way to detect if there is any file added in a folder? Include the sub-folder. For example, check if any text file *.txt is added in folder c:\data-files\ or its sub-folders. The folder can be shared folder of another machine too. 回答1: Perhaps you are confused on the types of events that are triggered: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher_events(v=vs.110).aspx This should work, taken from the link above and modified for your requirements: #By BigTeddy 05

Pad IP addresses with leading 0's - powershell

孤街醉人 提交于 2021-02-19 07:52:23
问题 I'm looking to pad IP addresses with 0's example 1.2.3.4 -> 001.002.003.004 50.51.52.53 -> 050.051.052.053 Tried this: [string]$paddedIP = $IPvariable [string]$paddedIP.PadLeft(3, '0') Also tried split as well, but I'm new to powershell... 回答1: You can use a combination of .Split() and -join . ('1.2.3.4'.Split('.') | ForEach-Object {$_.PadLeft(3,'0')}) -join '.' With this approach, you are working with strings the entire time. Split('.') creates an array element at every . character. .PadLeft

Pad IP addresses with leading 0's - powershell

最后都变了- 提交于 2021-02-19 07:52:07
问题 I'm looking to pad IP addresses with 0's example 1.2.3.4 -> 001.002.003.004 50.51.52.53 -> 050.051.052.053 Tried this: [string]$paddedIP = $IPvariable [string]$paddedIP.PadLeft(3, '0') Also tried split as well, but I'm new to powershell... 回答1: You can use a combination of .Split() and -join . ('1.2.3.4'.Split('.') | ForEach-Object {$_.PadLeft(3,'0')}) -join '.' With this approach, you are working with strings the entire time. Split('.') creates an array element at every . character. .PadLeft

Print a PDF to a file in PowerShell

半城伤御伤魂 提交于 2021-02-19 07:38:37
问题 I need to automatically print a PDF file to a file (need to have printer driver set all the print options like stapling, duplexing, etc) on a network folder so other employees can print the .prn file from networked printers. After a fair bit of searching I have found that it is possible to have PowerShell print the PDF using Start-Process -FilePath document.pdf -Verb Print which invokes the appropriate application to print the PDF but doesn't allow me to check the "Print to file" box. I could

Mocking class functions with Pester 5 and PowerShell 7

帅比萌擦擦* 提交于 2021-02-19 07:37:42
问题 Does anyone have an example of mocking a dot-sourced class function with Pester 5 and PowerShell 7? Thank you. Edit: example Classes\MyClass.ps1: class MyClass { [void] Run() { Write-Host "Class: Invoking run..." } } MyModule.psm1: # Import classes . '.\Classes\MyClass.ps1' # Instantiate classes $MyClass = [MyClass]::new() # Call class function $MyClass.Run() 回答1: Pester only mocks commands - not classes or their methods. The easiest way to "mock" a PowerShell class for method dispatch

How to create a new window and set UseShellExecute false in powershell?

末鹿安然 提交于 2021-02-19 07:35:12
问题 The requirement is a bit strange, I've encountered a strange stuck problem in multi-thread in powershell. So I want to create a new window and don't use shell Execute. But I cannot make it with below code, the window doesn't show up. $approot is desktop, in start.bat, just do "dir /s .\" I want the dir result shows in another window instead of the window execute this script, and I don't want use shell execute. $startInfo = New-Object system.Diagnostics.ProcessStartInfo $startInfo

PowerShell: Cannot find proper .ctor when the .ctor has only one argument of type List<T> [duplicate]

非 Y 不嫁゛ 提交于 2021-02-19 07:18:13
问题 This question already has answers here : How do I call New-Object for a constructor which takes a single array parameter? (2 answers) Closed 2 years ago . It seems that there is a bug in the O-O support in PowerShell . When instantiating an object of a class with a constructor that accepts a List< T > and this is the only parameter , PowerShell cannot find a proper constructor. Code sample: class MyClass { MyClass( #[int]$i, [System.Collections.Generic.List[string]] $theparams) { } }

PowerShell: Cannot find proper .ctor when the .ctor has only one argument of type List<T> [duplicate]

六月ゝ 毕业季﹏ 提交于 2021-02-19 07:16:47
问题 This question already has answers here : How do I call New-Object for a constructor which takes a single array parameter? (2 answers) Closed 2 years ago . It seems that there is a bug in the O-O support in PowerShell . When instantiating an object of a class with a constructor that accepts a List< T > and this is the only parameter , PowerShell cannot find a proper constructor. Code sample: class MyClass { MyClass( #[int]$i, [System.Collections.Generic.List[string]] $theparams) { } }

Visual Studio Code fails to open from Powershell

半世苍凉 提交于 2021-02-19 06:15:08
问题 I'm trying to open Visual Studio Code from Powershell using the code command while inside a folder. When I try to do this, I always get this error: Blockquote code : The term 'code' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + code . + ~~~~ + CategoryInfo : ObjectNotFound: (code:String) [], CommandNotFoundException +