powershell-2.0

When should I use Write-Error vs. Throw? Terminating vs. non-terminating errors

安稳与你 提交于 2019-11-27 10:49:23
Looking at a Get-WebFile script over on PoshCode, http://poshcode.org/3226 , I noticed this strange-to-me contraption: $URL_Format_Error = [string]"..." Write-Error $URL_Format_Error return What is the reason for this as opposed to the following? $URL_Format_Error = [string]"..." Throw $URL_Format_Error Or even better: $URL_Format_Error = New-Object System.FormatException "..." Throw $URL_Format_Error As I understand, you should use Write-Error for non-terminating errors, and Throw for terminating errors, so it seems to me that you should not use Write-Error followed by Return. Is there a

PowerShell: Create Local User Account

假装没事ソ 提交于 2019-11-27 10:39:51
问题 I need to create a new local user account, and then add them to the local Administrators group. Can this be done in PowerShell? EDIT: # Create new local Admin user for script purposes $Computer = [ADSI]"WinNT://$Env:COMPUTERNAME,Computer" $LocalAdmin = $Computer.Create("User", "LocalAdmin") $LocalAdmin.SetPassword("Password01") $LocalAdmin.SetInfo() $LocalAdmin.FullName = "Local Admin by Powershell" $LocalAdmin.SetInfo() $LocalAdmin.UserFlags = 64 + 65536 # ADS_UF_PASSWD_CANT_CHANGE + ADS_UF

How to import custom PowerShell module into the remote session?

折月煮酒 提交于 2019-11-27 10:17:48
问题 I'm developing a custom PowerShell module, which I'd like to use in context of a remote session to a different computer. The following code (which obviously doesn't work) explains what I'm trying to achieve: import-module .\MyCustomModule.psm1 $session = new-pssession -computerName server01 invoke-command -session $session -scriptblock { <# use function defined in MyCustomModule here #> } The first question is whether it is at all possible to achieve this scenario? I mean I would only like my

How to convert C# code to a PowerShell Script?

夙愿已清 提交于 2019-11-27 10:09:47
问题 I regularly have to convert an existing C# code snippet/.CS file to a PowerShell script. How could I automate this process? While I am aware that there are methods that can convert a .cs file to a cmdlet, I'm only interested in converting the C# code to a script or module. 回答1: I know you're looking for something that somehow converts C# directly to PowerShell, but I thought this is close enough to suggest it. In PS v1 you can use a compiled .NET DLL: PS> $client = new-object System.Net

PowerShell, Web Requests, and Proxies

五迷三道 提交于 2019-11-27 09:37:43
问题 When making a simple web request is there a way to tell the PowerShell environment to just use your Internet Explorer's proxy settings? My proxy settings are controlled by a network policy(or script) and I don't want to have to modify ps scripts later on if I don't have to. UPDATE: Great info from the participants. The final script template that I'll use for this will look something like the following: $proxyAddr = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet

What can I do with C# and Powershell?

二次信任 提交于 2019-11-27 09:25:01
问题 I have a decent understanding of C# and a very basic understanding of powershell. I'm using Windows PowerShell CTP 3, which has been really fun. But I want to go beyond writing scripts/functions. Is there any cool stuff to do with C#? 回答1: I think the most interesting thing you can do with C# and PowerShell is to build CmdLet's. These are essentially plugins to PowerShell that are written in managed code and act like normal functions. They have a verb-noun pair and many of the functions you

How to upgrade PowerShell version from 2.0 to 3.0

人走茶凉 提交于 2019-11-27 09:22:27
问题 The OS that I am using is Windows 7, and the PowerShell version that is installed here is 2.0. Is it possible for me to upgrade it to version 3.0 or 4.0? Because there are cmdlets that version 2.0 can't recognize. 回答1: Download and install from http://www.microsoft.com/en-us/download/details.aspx?id=34595. You need Windows 7 SP1 though. It's worth keeping in mind that PowerShell 3 on Windows 7 does not have all the cmdlets as PowerShell 3 on Windows 8. So you may still encounter cmdlets that

How to get all groups that a user is a member of?

做~自己de王妃 提交于 2019-11-27 09:10:34
问题 PowerShell's Get-ADGroupMember cmdlet returns members of a specific group. Is there a cmdlet or property to get all the groups that a particular user is a member of? I fixed my mistake: Get-Member should be Get-ADGroupMember . 回答1: Get-ADPrincipalGroupMembership will do this. Get-ADPrincipalGroupMembership username | select name name ---- Domain Users Domain Computers Workstation Admins Company Users Company Developers AutomatedProcessingTeam 回答2: Single line, no modules necessary, uses

Methods to hex edit binary files via Powershell

北战南征 提交于 2019-11-27 08:57:51
Am trying to perform binary hex edit from the command line using only powershell. Have had partial success performing a hex replace with this snip. Problem springs up when 123456 occurs multiple times and the replacement was only supposed to occur at a specific location. NOTE: The snip requires the Convert-ByteArrayToHexString and Convert-HexStringToByteArray functions found here. http://www.sans.org/windows-security/2010/02/11/powershell-byte-array-hex-convert $readin = [System.IO.File]::ReadAllBytes("C:\OldFile.exe"); $hx = Convert-ByteArrayToHexString $readin -width 40 -delimiter ""; $hx =

How do you comment out code in PowerShell?

天大地大妈咪最大 提交于 2019-11-27 08:56:05
问题 How do you comment out code in PowerShell (1.0 or 2.0)? 回答1: In PowerShell V1 there's only # to make the text after it a comment. # This is a comment in Powershell In PowerShell V2 <# #> can be used for block comments and more specifically for help comments. #REQUIRES -Version 2.0 <# .SYNOPSIS A brief description of the function or script. This keyword can be used only once in each topic. .DESCRIPTION A detailed description of the function or script. This keyword can be used only once in each