powershell-2.0

Set the encoding to ANSI in PowerShell 2.0

大城市里の小女人 提交于 2019-12-20 02:28:09
问题 I want to set the encoding of a file to ANSI using the parameter -Encoding of the Set-Content cmdlet, I tried this one but it didn't work: Set-Content -LiteralPath "$filePath" -Encoding Default 回答1: PowerShell v2 doesn't recognize an argument Default for the parameter -Encoding . Use the argument Ascii to save a file with ANSI encoding: Set-Content -LiteralPath "$filePath" -Encoding Ascii or omit the parameter entirely ( Set-Content defaults to ANSI encoding): Set-Content -LiteralPath "

Powershell 2.0 generates nulls between characters

独自空忆成欢 提交于 2019-12-19 14:58:33
问题 With powershell 2.0: write-output "abcd" >> mytext.txt returns: a nul b nul c nul d nul od -c shows the nul as a true binary zero, \0 , or: a \0 b \0 c \0 d \0 (and \r \0 \n \0 ). I am trying to generate some SQL, so I don't think this will do. Any ideas of what's going on and how to use write-output to just get the specified characters? 回答1: This is because write-output defaults to UTF-16 text encoding, which is 2 bytes per character. When you are dealing with text that fits into the ASCII

IE Com Object, Difference between methods .Navigate and .Navigate2

送分小仙女□ 提交于 2019-12-19 09:06:54
问题 What is the difference between $ie.Navigate("URL") and $ie.Navigate2("URL") ? Get-Member says: Navigate Method void Navigate (string, Variant, Variant, Variant, Variant) Navigate2 Method void Navigate2 (Variant, Variant, Variant, Variant, Variant) Sample Code: $ie = New-Object -ComObject InternetExplorer.Application $ie.visible = $true $ie.Navigate("www.stackoverflow.com") #or $ie.Navigate2("www.stackoverflow.com") 回答1: The difference is in the first argument URL . Here is what MSDN says:

How to Import system modules automatically?

痞子三分冷 提交于 2019-12-19 08:21:21
问题 I am using Windows 2008 r2 64 bit system. While testing our script ,we did right click on powershell (which is present in taskbar) and import system modules. Our scripts will be used while deployment. So in destination machine we are not sure whether all system modules are imported or not? How to make import all the system modules imported if not imported already? 回答1: There's a hidden powershell switch to load system modules: powershell.exe -ImportSystemModules System modules reside in the

Powershell INI editing

旧时模样 提交于 2019-12-19 07:34:49
问题 I want to changing some values in an INI file. Unfortunately, I have keys in 2 different sections which share an identical name but need different values. My code uses the Get-IniContent function from PsIni. Example INI file: [PosScreen] BitmapFile=C:\Temp\Random.bmp Bitmap=1 [ControlScreen] BitmapFile=C:\Temp\Random.bmp Bitmap=1 I need to change the above to the following: [PosScreen] BitmapFile=C:\Temp\FileC.bmp Bitmap=1 [ControlScreen] BitmapFile=C:\Temp\FileD.bmp Bitmap=1 The PowerShell

My powershell script is printing a '1' before my expected output. Why?

僤鯓⒐⒋嵵緔 提交于 2019-12-19 07:05:11
问题 I wrote a simple powershell script that checks the number of tickets in a PSA service board and outputs the number of tickets and a color associated with it. However, my script is printing a '1' before the results. I am very new to powershell, and I can't seem to find the line that would print this. Any help would be appreciated. Thanks $SqlServer = "server" $SqlCatalog = "database" $SqlQuery = "SELECT COUNT(*) FROM v_rpt_Service WHERE Board_Name = 'Traige' AND Parent = ''" $SqlConnection =

My powershell script is printing a '1' before my expected output. Why?

感情迁移 提交于 2019-12-19 07:05:02
问题 I wrote a simple powershell script that checks the number of tickets in a PSA service board and outputs the number of tickets and a color associated with it. However, my script is printing a '1' before the results. I am very new to powershell, and I can't seem to find the line that would print this. Any help would be appreciated. Thanks $SqlServer = "server" $SqlCatalog = "database" $SqlQuery = "SELECT COUNT(*) FROM v_rpt_Service WHERE Board_Name = 'Traige' AND Parent = ''" $SqlConnection =

Write-Host => Export to a file

会有一股神秘感。 提交于 2019-12-19 05:11:07
问题 I have got a script with some commands such as Write-Host "Server1" . How can I export it to a file? When I tried with script > export.txt it didn't work. 回答1: Write-Host redirects the output only to the console. You can use Write-Output and redirect to a file ( > export.txt or pipe to Out-File export.txt ) In the extreme case when you absolutely need to redirect all output from a script, take a look to this cmdlet: Start-Transcript Get-Help Start-Transcript -full 回答2: In PowerShell script >

get Current user context

给你一囗甜甜゛ 提交于 2019-12-19 03:06:09
问题 I've got problems with running a powershellscript from different locations (c# application, webservice...). I think it is a user context problem, so now I'm trying to find out under which user context powershell script is running. Is there any possibility log the current usercontext of the powershellscript? 回答1: If you need to know the actual user: [reflection.assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement") [System.DirectoryServices.AccountManagement.UserPrincipal

How can I get PowerShell Added-Types to use Added Types

亡梦爱人 提交于 2019-12-18 17:15:22
问题 I'm working on a PoSh project that generates CSharp code, and then Add-Type s it into memory. The new types use existing types in an on disk DLL, which is loaded via Add-Type. All is well and good untill I actualy try to invoke methods on the new types. Here's an example of what I'm doing: $PWD = "." rm -Force $PWD\TestClassOne* $code = " namespace TEST{ public class TestClassOne { public int DoNothing() { return 1; } } }" $code | Out-File tcone.cs Add-Type -OutputAssembly $PWD\TestClassOne