powershell-2.0

Test WebClient API call

与世无争的帅哥 提交于 2019-12-12 05:39:25
问题 I have the following PowerShell script that makes an API Rest GET call. $FullURL = "http://test.net/config/server/$($env:COMPUTERNAME)?format=test" $API = New-Object System.Net.WebClient $APIData = $API.DownloadString($FullURL) Set-Content -Value $APIdata -Path $APIDataFile -Force The call is dependent on the local hostname in the URI. It gets the data and exports to a text file as a backup. The problem - the API host maybe down or no information available for the host which will cause all

How to add key/value pair at the end of hash table?

僤鯓⒐⒋嵵緔 提交于 2019-12-12 05:38:32
问题 I am trying to calculate code count using PowerShell script. I found a script on the Internet and am trying to add the total line at the end. I have added the column $CountHash.Add("Total", $Total) at the end. Param( [string]$path, [string]$outputFile, [string]$include = "*.*", [string]$exclude = "") Clear-Host $Files = Get-ChildItem -re -in $include -ex $exclude $path $CountHash = @{} $Total=0 Foreach ($File in $Files) { #Write-Host "Counting $File.FullName" $fileStats = Get-Content $File

Delete user profile using Powershell

笑着哭i 提交于 2019-12-12 05:34:53
问题 I am wondering how I can delete user profile by using Powershell? I know the command of Get-WmiObject Win32_UserProfile which will give me the whole users on the computer. I have 2 variables of $computername and $username . So I wants to use the above command to delete on a remote computer (which is $computername ) the profile of $username . How I can do it? Thanks. 回答1: Get-WMIObject can retrieve objects from remote computers with no problem, and not only does the Win32_UserProfile class

Passing parameters in PowerShell 2.0

不羁的心 提交于 2019-12-12 04:54:45
问题 I have the following test defined in a psake build script: task package -depends create_wix_content_fragment { & $candle -dProductName=Foo ` -dVersion=$version ` -dProductID=0cd64670-5769-4e34-8b21-c6242e7ca5a2 ` -dUpgradeCode=307601e9-4eea-4b5c-938a-354115d5c419 ` -dAppPool=FooAppPool ` -dInstallDirectory=Foo ` -ext WixIISExtension ` -ext WixUIExtension ` $wix_shell ` $build_output_dir\WebContent.wxs } For some reason Powershell passes the $version variable as a literal string "$version"

Enabling desktop experience from C# on windows 2008 / 2012

懵懂的女人 提交于 2019-12-12 04:42:41
问题 I am currently using command: servermanagercmd -install Desktop-Experience From what I read, I understood that this is no longer supported in Windows 2012. I understood that can be done from PowerShell cmdlet somehow but can't find an example for a need. 回答1: I think the PS script you're looking for is: Add-WindowsFeature Desktop-Experience 回答2: I don't think the Add-WindowsFeature command will work back in 2008. DISM is the best route for all versions involved. Dism /online /Enable-Feature

preprocessor/ macro expansion with powershell

无人久伴 提交于 2019-12-12 04:07:05
问题 From time to time, I use write-host in my scripts to do poor-man debugging/ logging, but having all of them fire on each run clutters my screen with tons of unneeded info. Of course, I can comment/ uncomment the ones I need for the current run but this is a lot of editing and very error-prone. here is what I finally came to: [cmdletbinding()] param( [parameter()] [string[]]$dbg_points=@('*') ) $debug = ($PSCmdlet.myInvocation.BoundParameters['Debug'].isPresent -eq $true) $dbg_all = $dbg

Powershell select-string substring

狂风中的少年 提交于 2019-12-12 03:57:46
问题 I am currently reading the contents of a file and returning a pattern which gets one line. I am able to get the correct line by itself now. How do I do a substring function on the $RunTimeLine result to return the first four characters? [string] $RunTimeLine = Select-String -Path $IDCSwiftFlowXMLFile -pattern "<time>" | ForEach-Object {$_.Line} OUTPUT of $RunTimeLine >>>>>>> 30 5 * * 2-6 2 I have attempted doing the following but it isn't returning anything $StartTime = $RunTimeLine.substring

Matching Lines in a text file based on values in CSV

ⅰ亾dé卋堺 提交于 2019-12-12 03:56:24
问题 Hi Everyone, I am having trouble with the below script. Here is the requirement: 1) Each text file needs to be compared with a single CSV file. The CSV file contains the data to that if present in the text file should match. 2) If the data in the text file matches, output the matches only and run jobs etc.. 3) If the text file has no matches to the CSV file, exit with 0 as no matches are found. I have tried to do this, but what I end up with is matches, and also non matches. What I really

Send -MailMessage -Attachments will not accept null for any variables

孤街醉人 提交于 2019-12-12 03:38:32
问题 I'm using the Send-MailMessage function in Powershell v2.0 I'm using variables for the attachments as there will not always be attachments being sent. If there are no attachments (file locations) in the variables, I get an error otherwise it works. How I can I setup the Send-MailMessage function to output attachments and at other times not without it failing. Send-MailMessage -BodyAsHtml –From Monitoring@CorporateActions -Priority $Priority –To "steven.meadows@dionglobal.eu" -Attachments

Stream CMD output to Select-String

☆樱花仙子☆ 提交于 2019-12-12 03:32:59
问题 Is there a way to fluidly stream CMD standard output to PowerShell to Select-String ? I have tried every method I can find, but every way I find stores the info in a variable before piping it to be worked with. It has to be a stream because the standard out can be well over 20GBs in size and ends up causing an out of memory exception before the process crashes. Here is the command I have been using: gunzip.exe -c "*_2015-06-05_*" |powershell.exe "& {$input | select-string -pattern '1292681581