powershell-4.0

Can I make this script even faster?

强颜欢笑 提交于 2019-12-10 11:31:32
问题 I wrote a simple script for an internship, which trawls through a provided directory and deletes any file older than a specified number of days. I've spent all my free time today attempting to tighten it up. Here's what I've got so far: function delOld($dir, $numDays){ $timespan = new-timespan -days $numDays $curTime = get-date get-childItem $dir -Recurse -file | where-object {(($curTime)-($_.LastWriteTime)) -gt $timespan} | remove-Item -whatif } Here is an example of a call of the function:

Powershell: How to I test if a line of text contains line feed or carriage return?

喜你入骨 提交于 2019-12-10 10:58:58
问题 How do I test if the first line of a text file is terminated with \r or \n? I tried various renditions similar to the following. I'm not sure that the string imported into powershell (first line of the file) even contains the invisible characters. I also tried using the StreamReader method to read the line to no avail. $master = Get-Content $masterFilename | Select-Object -First 1 if ($master.Contains("\r|\n")) { write-host "you betcha, there's a carriage return or line feed" } Thank you. 回答1

PsObject array in powershell

拥有回忆 提交于 2019-12-10 03:41:50
问题 This is my code : $a = @() for ($i = 0; $i -lt 5; $i++) { $item = New-Object PSObject $item | Add-Member -type NoteProperty -Name 'Col1' -Value 'data1' $item | Add-Member -type NoteProperty -Name 'Col2' -Value 'data2' $item | Add-Member -type NoteProperty -Name 'Col3' -Value 'data3' $item | Add-Member -type NoteProperty -Name 'Col4' -Value 'data4' $a += $item } With this code I'm able to have a result like this: Col1 Col2 Col3 Col4 ---- ---- ---- ---- 0 1 2 3 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7

Launch Elevated CMD.exe from Powershell

一曲冷凌霜 提交于 2019-12-09 19:00:25
问题 I am trying to launch an elevated CMD window from PowerShell but I am running into some issues. Below is the Code I have now. There is an admin account on the machine that has the username of "test" and a Password of "test" $username = "test" $password = ConvertTo-SecureString "test" -AsPlainText -Force $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password Start-Process "cmd.exe" -Credential $cred This is all working fine for running an

How to convert string to table or objects in powershell

泪湿孤枕 提交于 2019-12-09 03:39:40
问题 How can I convert the below exported text to csv so that i can use it as objects in powershell. Eg: where{$_.qlimit -eq 27} Text: samid qlimit qused Administrator -1 0 Guest -1 0 admin 27 8 krbtgt -1 0 r -1 0 admin2 44 0 回答1: Use the Get-Content cmdlet to load the file, replace two or more whitespaces with a comma and convert it to CSV using the ConvertFrom-Csv cmdlet: $object = (Get-Content 'your_file').Trim() -replace '\s{2,}', ',' | ConvertFrom-Csv If you now query your object: $object |

How to check if an associative array is empty in powershell

风流意气都作罢 提交于 2019-12-08 15:55:59
问题 $a = @() How do I check if $a above is empty (which it is). I would like to get $true as answer. 回答1: That's not an associative array, it's a regular array, but the answer is the same. Use .Count and compare to 0. An associative array is called a [hashtable] in PowerShell and its literal form uses @{} (curly braces). @{}.Count -eq 0 # hashtable (associative array) @().Count -eq 0 # array 回答2: Arrays have Count property, and you can check if this value is 0. So the condition you would check

How to elevate an already running session within its own code

主宰稳场 提交于 2019-12-07 23:57:58
问题 I'm writting a PowerShell script that configures some things in Active Directory. I need to run it as a specific user in order to get the right permissions for the process, currently i'm running the .ps1 file through a .bat file, so I can choose "run as a different user" or "run as administrator". What I'm tryng to achieve is that inside the script I will ask the user for the right credentials, and then elevate the session to run with the inputed user creds. I've tried using this within my

Attach Debugger to multiple processes via powershell

天大地大妈咪最大 提交于 2019-12-07 22:50:18
问题 I have several processes running that I'd like to attach to the VS debugger via powershell. Currently, I can do this: Get-Process NServiceBus.Host | Debug-Process If there is only one process, then I am prompted to select the correct debugger and I can continue. However, if there are more than one processes, when I'm prompted to select a debugger for the second process, I am unable to select the currently running instance of Visual Studio. How can I use powershell to attach multiple processes

The PowerShell provider xWebAdministration does not exist at the PowerShell module path nor is it registered as a WMI provider

人走茶凉 提交于 2019-12-07 18:29:58
问题 I have made this very simple DSC script. Which basically creates web application in default website. Configuration ConfigureWebApp { param ($MachineName) Import-DscResource -Module xWebAdministration Node $MachineName { xWebApplication NewWebApplication { Name = "MyApp" Website = "Default Web Site" WebAppPool = "DefaultAppPool" PhysicalPath = "C:\Inetpub\wwwroot\MyApp" Ensure = "Present" } } } cd "C:\Dsc\scripts" ConfigureWebApp -MachineName "WIN-KPURIN2B87H" When I run generated MOF file, it

powershell script reading parameters from txt

霸气de小男生 提交于 2019-12-07 16:38:20
问题 I have a script that takes 2 parameters (name and location). I put the name and location into a txt file as per this post here Powershell parameters from file. I got prompted to put in value for the 2nd parameter: Import-Csv 'C:\temp\paramtest.txt' | % { C:\temp\script\paramtest.ps1 @_ } cmdlet paramtest.ps1 at command pipeline position 1 Supply values for the following parameters: param2:** This is what my .txt look like: "param","param2" "foo","c:\temp" "bar","c:\temp" "foobar","c:\temp"