powershell-2.0

Powershell - SetForegroundWindow

倾然丶 夕夏残阳落幕 提交于 2019-12-07 12:02:57
问题 With a powershell code I try to change the position of a window (is works correctly) and put this windows "Always on top". Please find below my code: Import-Module C:/install/WASP/wasp.dll for($i=1; $i -le 300000; $i++) { $allWindow = Select-Window MyCheck* if($allWindow) { foreach ($currentWindow in $allWindow) { $positionWindow = WindowPosition $currentWindow foreach ($currentPosition in $positionWindow) { #if we find the correct windows if ( $currentWindow.title -match "\([0-9]*\)#" ) {

PowerShell command to replace a chunk of text in a file

不打扰是莪最后的温柔 提交于 2019-12-07 11:38:39
问题 I am trying to replace a chunk of text in a file using PowerShell. For instance, I have a .sql file and I know the exact chunk of an SQL script that needs to be replaced in that particular file. After reading through some of the PowerShell replace examples it looks like PowerShell returns the content of the file in an array (each line representing one entry in the array). For example: GO :on error exit GO IF (DB_ID(N'$(DatabaseName)') IS NOT NULL) BEGIN ALTER DATABASE [$(DatabaseName)] SET

Powershell logging from invoke-command

早过忘川 提交于 2019-12-07 11:36:31
问题 I have a script that includes a function for logging. The function writes log to $msg variable then writes the message to the screen. i can redirect this output to a .txt file for logging. I need to run the script to multiple servers using invoke-command and write the log output to a txt file on the computer running the script. I am trying to figure out how to output $msg in the write-log function from the PSSession and return it so I can create a master log file for all the servers. Should I

Powershell array scope; why is my array empty?

不想你离开。 提交于 2019-12-07 07:11:22
问题 I want to have an array and add elements to it from different functions in my script. My example below illustrates where I might be misunderstanding something regarding scope. My understanding currently is that if an array is defined outside the function, and then elements are added inside the function, those elements should be available outside the function. Function ListRunningServices { $services+= Get-Service | ?{$_.Status -eq "Running"} | sort Name | select Name; } $services = @();

How to access NoteProperties on the InputObject to a remoting session

*爱你&永不变心* 提交于 2019-12-07 05:36:03
问题 I'm having difficulty accessing PSObject s with NoteProperties defined, that are being passed through to a remote session via Invoke-Command . The reason I am constructing an object for this purpose is that I need access to two separate pieces of information in the remote session, but PowerShell only provides for a single InputObject . My code looks something like this: $sessionInput = new-object -TypeName System.Object $sessionInput | Add-Member -MemberType NoteProperty -Name NoteProperty1

How to Return a Pretty Zero Value with the Count Property from Get-Process?

喜夏-厌秋 提交于 2019-12-07 04:39:45
问题 Compare the three scripts below: Sample 1 $a = GPS | Where {$_.ProcessName -Match 'AcroRd32'} $a $a.Count If ($a.Count -Eq 0) { Echo "Adobe Reader is Off" } Else { Echo "Adobe Reader is On" } # If Adobe Reader is not running, how come 0 (zero) is not returned? # This is prettier, should I use it? Or does it slow down performance? Sample 2 $a = GPS AcroRd32 $a $a.Count If ($a.Count -Eq 0) { Echo "Adobe Reader is Off" } Else { Echo "Adobe Reader is On" } # If Adobe Reader is not running, how

How can I convert a hastable to a json string in powershell?

▼魔方 西西 提交于 2019-12-07 04:37:38
问题 I'm trying to convert a hashtable to a json object for use in a web service with powershell 2.0. $testhash = @{ Name = 'John Doe' Age = 10 Amount = 10.1 MixedItems = (1,2,3,"a") NestedHash = @{ nestedkey = "nextedvalue" } } function toJson($obj){ $ms = New-Object IO.MemoryStream $type = $obj.getType() [Type[]]$types = ($obj | select -expand PsTypeNames | Select -unique) + [type]'System.Management.Automation.PSObject' $js = New-Object System.Runtime.Serialization.Json

Creating a PowerShell Connection String

拜拜、爱过 提交于 2019-12-07 02:35:30
I have been trying to create a ConnnectionString that will allow me to connect to my local database using PowerShell. Below is my code: $conn = New-Object System.Data.SqlClient.SqlConnection $conn.ConnectionString = "Server=localhost;Database=test;Uid=<username here>;Pwd=<password here>;" $conn.Open() $sql = "SELECT EMP_STATUS FROM test_table" $cmd = New-Object System.Data.SqlClient.SqlCommand($sql,$conn) $rdr = $cmd.ExecuteReader() while($rdr.Read()) { $test = $rdr["EMP_STATUS"].ToString() } Write-Output $test However, I have NO CLUE what I am doing wrong and have been pulling my hair out for

Reference command name with dashes

你。 提交于 2019-12-07 00:10:15
问题 I've recently discovered that Powershell functions are just named scriptblocks. For example function HelloWorld { Write-Output "Hello world" } $hw = $function:HelloWorld & $hw Will execute the HelloWorld method. However, what I have not been able to figure out, is how to get a reference to a method that has a dash in it's name: function Hello-World { Write-Output "Hello world" } $hw = $function:Hello-World You must provide a value expression on the right-hand side of the '-' operator. At line

Pass a function (with arguments) as a parameter in PowerShell

拥有回忆 提交于 2019-12-06 22:07:06
问题 I've been successfully passing no-argument functions around in PowerShell using ScriptBlocks. However, I can't get this to work if the function has arguments. Is there a way to do this in PowerShell? (v2 preferably) Function Add([int] $x, [int] $y) { return $x + $y } Function Apply([scriptblock] $s) { write-host ($s.Invoke(1,2)) } Then Apply { Add } writes 0 to the console. Apply does invoke Add , but doesn't pass any arguments in (i.e. uses the default [int] values of 0 and 0) 回答1: Ok, I