powershell-2.0

Powershell: Debug in Production / Good Exception Handling

断了今生、忘了曾经 提交于 2019-12-04 02:09:45
问题 What is the best way to analyze powershell cmdlets in production ? Suppose you write a script which does the following - Write lof of registry values Register COM Dlls Make IIS AppPools Start Windows Services &...something goes wrong in between, then what are the best practices to inform user such that root issue can be traced and debugged ? Suppose, user credentials fail for AppPool creation for some reason and I want to stop processing at that time plus I want to rollback what I had done

PowerShell script to move files and folders including subfolders from one location to another older than x days

冷暖自知 提交于 2019-12-04 01:03:59
I developed a PowerShell script, and it's working absolutely fine. The only challenge is the files in the subfolders are not getting moved to the destination. get-childitem -Path "\\servername\location" | where-object {$_.LastWriteTime -lt (get-date).AddDays(-31)} | move-item -destination "C:\Dumps" I am unable to customize the script further. Musaab Al-Okaidi Use the -Recurse option on the Get-ChildItem command to get through to the files in the sub folders and then move each individually by piping the collection to Move-Item Get-ChildItem -Path "C:\Test" -Recurse | Where-Object {$_

Why is Powershell ISE showing errors that Powershell console does not show?

半城伤御伤魂 提交于 2019-12-04 00:19:23
I'm running exactly the same script.ps1 file in a Powershell ISE (manually loading the script and pressing F5) and in a Powershell console (executing the script file). They both work but ISE shows errors that the console does not. Why? The code is: git push origin master Write-Host "lastExitCode: $lastExitCode Last command was successful: $?" This code output this error in the ISE: git.cmd : Initializing to normal mode At E:\script.ps1:28 char:4 + git <<<< push origin master + CategoryInfo : NotSpecified: (Initializing to normal mode:String) [], RemoteException + FullyQualifiedErrorId :

Find and replace in files fails

瘦欲@ 提交于 2019-12-03 23:34:15
I am trying to do find and replace in a file using following approach. Function Find-Replace ($FileFullpath, $FindString, $ReplacementString) { Get-Content $FileFullpath | Foreach-Object {$_ -replace $FindString, $ReplacementString } | Set-Content $FileFullpath } Find-Replace "c:\program files (x86)\MyProj\web.config" $OldServiceName $NewServiceName But i am always getting error. Set-Content : The process cannot access the file 'c:\program files (x86)\MyProj\web.config' because it is being used by another process. The file is not opened any where. I think Get-content is yet to release the file

Use the Get-Help cmdlet to display comment-based help in the same format

醉酒当歌 提交于 2019-12-03 22:31:26
I am trying to use the Get-Help cmdlet to display comment-based help in the same format in which it displays the cmdlet help topics that are generated from XML files. The ability to do this is documented in about_Comment_based_Help on TechNet, but when I execute the get-help cmdlet against my script I only get the script name returned. Any help would be appreciated! PS C:\Admin> Get-Help .\checksystem.ps1 -full checksystem.ps1 checksystem.ps1 script: function IsAlive { <# .DESCRIPTION Checks to see whether a computer is pingable or not. .PARAMETER computername Specifies the computername.

Exporting Collection of Hashtable data to CSV

老子叫甜甜 提交于 2019-12-03 16:39:29
I am trying to export to CSV the name/value pairs of a collection hashtable items. The I have not found the correct syntax for the select-object portion of the code. I would the CSV file to have columes for Url and Owner. Thanks for the help [System.Collections.ArrayList]$collection = New-Object System.Collections.ArrayList($null) $SiteInfo = @{}; $SiteInfo.Url = "http://some.url.com"; $SiteInfo.Owner = "Joe Smith"; $collection.Add($SiteInfo); $SiteInfo.Url = "http://another.url.com"; $SiteInfo.Owner = "Sally Jones"; $collection.Add($SiteInfo); $collection | foreach{ $hashTableDate = $_;

Can I determine if a PowerShell function is running as part of a pipeline?

老子叫甜甜 提交于 2019-12-03 16:16:15
问题 Can a PowerShell function determine if it is being run as part of a pipeline? I have a function which populates an array with instances of FileInfo which I would like to "yield" to the pipeline if the function is being run this way or produce some pretty output if the function is being invoked by itself from the command line. function Do-Something { $file_infos = @() # Populate $file_infos with FileInfo instances... if (INVOKED_IN_PIPELINE) { return $file_infos } else { foreach ($file_info in

How to extract data from a text file using R or PowerShell?

﹥>﹥吖頭↗ 提交于 2019-12-03 16:14:22
I have a text file containing data like this: This is just text ------------------------------- Username: SOMETHI C: [Text] Account: DFAG Finish time: 1-JAN-2011 00:31:58.91 Process ID: 2028aaB Start time: 31-DEC-2010 20:27:15.30 This is just text ------------------------------- Username: SOMEGG C: [Text] Account: DFAG Finish time: 1-JAN-2011 00:31:58.91 Process ID: 20dd33DB Start time: 12-DEC-2010 20:27:15.30 This is just text ------------------------------- Username: SOMEYY C: [Text] Account: DFAG Finish time: 1-JAN-2011 00:31:58.91 Process ID: 202223DB Start time: 15-DEC-2010 20:27:15.30 Is

How to copy folder with subfolders? [duplicate]

十年热恋 提交于 2019-12-03 15:34:07
问题 This question already has answers here : How to use PowerShell copy-item and keep structure (9 answers) Closed 2 years ago . This script works perfectly in PowerShell. It copies all files with specific type. But I want copy files with it folders & subfolders. $dest = "C:\example" $files = Get-ChildItem -Path "C:\example" -Filter "*.msg" -Recurse foreach ($file in $files) { $file_path = Join-Path -Path $dest -ChildPath $file.Name $i = 1 while (Test-Path -Path $file_path) { $i++ $file_path =

Create a manifest for nested PowerShell modules

佐手、 提交于 2019-12-03 14:17:18
I'm having a minor issue with nested, PowerShell modules. Get-Module correctly identifies the ExportedCommands , but the ModuleType is listed as Script , rather than Manifest : PS>get-module ModuleType Name ExportedCommands ---------- ---- ---------------- Script Bar Get-Bar Script Foo Get-Foo Directory structure: |-Modules |-Foobar |-Foobar.psd1 |-Bar |-Bar.psm1 |-Foo |-Foo.psm1 Foobar.psd1: ... # Script module or binary module file associated with this manifest ModuleToProcess = '' # Modules to import as nested modules of the module specified in ModuleToProcess NestedModules = 'Foo\Foo.psm1'