powershell-2.0

Add reference to dll in powershell 2.0

守給你的承諾、 提交于 2019-11-27 03:16:34
问题 I created a dll in C# and would like to use it in PowerShell. I know I can load the dll using: [Reflection.Assembly]::LoadFile("MyDll.dll") But I don't want to use reflection. Is there a simple way to do include my dll without reflection? Something like add reference to this dll? 回答1: In PowerShell 2.0 the cmdlet Add-Type is designed for this, for example: Add-Type -Path "$env:Xyz\bin\Npgsql.dll" (it’s more likely that under the covers it calls the same LoadFile but this way is more

powershell is missing the terminator: "

笑着哭i 提交于 2019-11-27 03:01:55
问题 I have the following script code #[string]$password = $( Read-Host "Input password, please" ) param ( [string]$ReleaseFile = $(throw "-ReleaseFile is required"), [string]$Destination = $(throw "-Destination is required") ) function unzipRelease($src, $dst) { $shell = new-object -com shell.application $zip = $shell.NameSpace($src) foreach($item in $zip.items()) { $shell.Namespace($dst).copyhere($item) } } # .\deployrelease.ps1 -ReleaseFile ".\deploy.zip" -Destination "." unzipRelease –Src '

How to fetch an attribute value from xml using powershell?

余生颓废 提交于 2019-11-27 02:33:31
问题 I have a list of XML files, from which I have to get the string after a particular line. In the files, I need to look for a tag Event and get the attribute value DLLRoutine . e.g. the tag would look something like below ... <Event Definition="Validate" DLLPath="" DLLName="Helper.dll" DLLClass="HelpMain" DLLRoutine="pgFeatureInfoOnValidate_WriteToRegSelectedFeatures" InputParameters="pTreeViewFeatureTreeServerOS" RunOnce="no"/> I just need to get Dllroutine values. How to do it using

Unable to exclude directory using Get-ChildItem -Exclude parameter in Powershell

二次信任 提交于 2019-11-27 02:33:02
问题 I am using Powershell v 2.0. and copying files and directories from one location to another. I am using a string[] to filter out file types and also need to filter out a directory from being copied over. The files are being filtered out correctly, however, the directory I am trying to filter obj keeps being copied. $exclude = @('*.cs', '*.csproj', '*.pdb', 'obj') $items = Get-ChildItem $parentPath -Recurse -Exclude $exclude foreach($item in $items) { $target = Join-Path $destinationPath $item

How to strip illegal characters before trying to save filenames?

给你一囗甜甜゛ 提交于 2019-11-27 01:52:30
问题 I was able to find how to use the GetInvalidFileNameChars() method in a PowerShell script. However, it seems to also filter out whitespace (which is what I DON'T want). EDIT: Maybe I'm not asking this clearly enough. I want the below function to INCLUDE the spaces that already existing in filenames. Currently, the script filters out spaces. Function Remove-InvalidFileNameChars { param([Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]

Obtaining ExitCode using Start-Process and WaitForExit instead of -Wait

旧街凉风 提交于 2019-11-27 00:48:44
I'm trying to run a program from PowerShell, wait for the exit, then get access to the ExitCode, but I am not having much luck. I don't want to use -Wait with Start-Process , as I need some processing to carry on in the background. Here's a simplified test script: cd "C:\Windows" # ExitCode is available when using -Wait... Write-Host "Starting Notepad with -Wait - return code will be available" $process = (Start-Process -FilePath "notepad.exe" -PassThru -Wait) Write-Host "Process finished with return code: " $process.ExitCode # ExitCode is not available when waiting separately Write-Host

Run N parallel jobs in powershell

孤街浪徒 提交于 2019-11-27 00:38:41
问题 I have the following powershell script $list = invoke-sqlcmd 'exec getOneMillionRows' -Server... $list | % { GetData $_ > $_.txt ZipTheFile $_.txt $_.txt.zip ... } How to run the script block ( { GetDatta $_ > $_.txt ....} ) in parallel with limited maximum number of job, e.g. at most 8 files can be generated at one time? 回答1: The Start-Job cmdlet allows you to run code in the background. To do what you'd ask, something like the code below should work. foreach ($server in $servers) { $running

How do I convert an array object to a string in PowerShell?

女生的网名这么多〃 提交于 2019-11-26 23:54:14
问题 How can I convert an array object to string? I tried: $a = "This", "Is", "a", "cat" [system.String]::Join(" ", $a) with no luck. What are different possibilities in PowerShell? 回答1: $a = 'This', 'Is', 'a', 'cat' Using double quotes (and optionally use the separator $ofs ) # This Is a cat "$a" # This-Is-a-cat $ofs = '-' # after this all casts work this way until $ofs changes! "$a" Using operator join # This-Is-a-cat $a -join '-' # ThisIsacat -join $a Using conversion to [string] # This Is a

Powershell. Create a class file to hold Custom Objects?

六眼飞鱼酱① 提交于 2019-11-26 22:48:17
问题 I use Powershell's custom-object command to hold data points. Custom-object creates just one object and assigns a variable to it. Can Powershell go one step further and create new classes from which objects can be made? In the examples below, I store three pieces of data: a server name, a timestamp, and the minutes since an event occurred on the server. When I was learning Powershell, I put all this into a two-dimensional array: $record = @("Server","Timestamp","Minutes") for ($j = 0; $j -lt

Get remote registry value

本秂侑毒 提交于 2019-11-26 22:22:56
I have the below script that I want it to go out to multiple servers and get the value of a registry. Unfortunately it is currently just posting back the local registry value of the machine that I am running the script on. How do I get the script to run against remote registry? SCRIPT: clear #$ErrorActionPreference = "silentlycontinue" $Logfile = "C:\temp\NEWnetbackup_version.log" Function LogWrite { param([string]$logstring) Add-Content $Logfile -Value $logstring } $computer = Get-Content -Path c:\temp\netbackup_servers1.txt foreach ($computer1 in $computer){ $Service = Get-WmiObject Win32