powershell-2.0

Launching background tasks in a remote session that don't get killed when the session is removed

寵の児 提交于 2019-11-27 23:52:44
问题 I have been using PsExec -d to launch console applications in a remote powershell session because I want these apps to run in the background while I perform some task. The problem is that I want the background applications to continue running even if I kill the remote powershell session with Remove-PSSession . What happens currently is once the remote powershell session is killed so are all the processes that were started with the help of PsExec -d . I'm guessing it has something to do with

Determine if script is running hidden

我们两清 提交于 2019-11-27 22:51:45
问题 I am trying to programmatically determine if a .ps1 script is running visibly or not. If it is running visibly, it should restart itself hidden. If it is already hidden, take no action. The problem I have is a loop where it continually restarts itself because hidden status cannot be determined. I've been looking at both get-process cmdlet and GWMI Win32_process and see nothing like a .visible property to check status. If ($me -eq visible ???) { $Invisible = New-Object System.Diagnostics

Extract the filename from a path

不羁的心 提交于 2019-11-27 22:38:57
I want to extract filename from below path: D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv Now I wrote this code to get filename. This working fine as long as the folder level didn't change. But in case the folder level has been changed, this code need to rewrite. I looking a way to make it more flexible such as the code can always extract filename regardless of the folder level. ($outputFile).split('\')[9].substring(0) If you are ok with including the extension this should do what you want. $outputPath = "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv"

Calling generic static method in PowerShell

六月ゝ 毕业季﹏ 提交于 2019-11-27 21:59:13
How do you call a generic static method of a custom class in Powershell? Given the following class: public class Sample { public static string MyMethod<T>( string anArgument ) { return string.Format( "Generic type is {0} with argument {1}", typeof(T), anArgument ); } } And this is compiled into an assembly 'Classes.dll' and loaded into PowerShell like this: Add-Type -Path "Classes.dll" What's the easiest way to call the MyMethod method? You can call generic methods, refer to the post Invoking Generic Methods on Non-Generic Classes in PowerShell . This is not straightforward, you need to use

Load variables from another powershell script

孤者浪人 提交于 2019-11-27 21:04:56
问题 I have several scripts that could be reusing variables so I'd like to isolate variables in their own Variables.ps1 script, i.e. $var1 = "1" $var2 = "2" I'm trying to load these variables then print them out in the Main.ps1 script like this: .\Variables.ps1 $var1 $var2 This works if I first run .\Variables.ps1 but not if I just run Main.ps1. My environment is PowerShell ISE. What am I doing wrong? 回答1: The variables declared in Variables.ps1 are at "Script Scope". That is you can not see them

Powershell 2 copy-item which creates a folder if doesn't exist

吃可爱长大的小学妹 提交于 2019-11-27 20:49:54
问题 $from = "\\something\1 XLS\2010_04_22\*" $to = "c:\out\1 XLS\2010_04_22\" copy-item $from $to -Recurse This works if c:\out\1 XLS\2010_04_22\ does exist . Is it possible with a single command to create c:\out\1 XLS\2010_04_22\ if it doesn't exist? 回答1: Yes, add the -Force parameter. copy-item $from $to -Recurse -Force 回答2: In PowerShell 2.0, it is still not possible to get the Copy-Item cmdlet to create the destination folder, you'll need code like this: $destinationFolder = "C:\My Stuff

How do I write to standard error in PowerShell?

别来无恙 提交于 2019-11-27 19:17:30
I'm having trouble figuring out how to both echo to the standard error stream and redirect the error stream of an executable. I have come from a Bourne shell and Korn shell background, of which I would use; # Write to stderr echo "Error Message!" >&2 # Redirect stderr to file /do/error 2>/tmp/err.msg Use Write-Error to write to stderr. To redirect stderr to file use: Write-Error "oops" 2> /temp/err.msg or exe_that_writes_to_stderr.exe bogus_arg 2> /temp/err.msg Note that PowerShell writes errors as error records. If you want to avoid the verbose output of the error records, you could write out

Handle command prompt error within PowerShell script

别说谁变了你拦得住时间么 提交于 2019-11-27 18:10:00
问题 I am trying to run a few command prompt commands like schtasks within a PowerShell script. I would like to know how to handle errors thrown by the command in PowerShell. I tried: & cmd.exe /c 'schtasks /Query /TN xx || echo ERROR' & cmd.exe /c 'ping.exe 122.1.1.1 && exit 0 || exit 1' Invoke-Expression -Command:$command I'm unable to run these commands and ignore or catch exception in a try..catch block of the PowerShell script. I know there are libraries but I'm limited to PowerShell v2.0.

How to execute powershell commands from a batch file?

ぃ、小莉子 提交于 2019-11-27 18:07:22
I have a PowerShell script to add a website to a Trusted Sites in Internet Explorer: set-location "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" set-location ZoneMap\Domains new-item TESTSERVERNAME set-location TESTSERVERNAME new-itemproperty . -Name http -Value 2 -Type DWORD I want to execute these PowerShell commands from a batch file. It seems simple when I have to run a single command, BUT in this case I have a sequence of related commands. I want to avoid creating a separate file for the PS script to be called from the batch - everything must be in the batch file. The

How to check Network port access and display useful message?

牧云@^-^@ 提交于 2019-11-27 18:01:26
I was trying to check whether the port is opened or not using powershell like follows. (new-object Net.Sockets.TcpClient).Connect("10.45.23.109", 443) This method works , but the output is not user-friendly. It means if there are no errors then it has access. Is there any way to check for success and display some message like " Port 443 is operational"? SimonOzturk If you're running Windows 8/Windows Server 2012 or newer, you can use the Test-NetConnection command in PowerShell. Ex: Test-NetConnection -Port 53 -ComputerName LON-DC1 I improved Salselvaprabu's answer in several ways: It is now a