powershell-2.0

Administrative privileges

穿精又带淫゛_ 提交于 2019-12-12 22:40:45
问题 In a thread here I found a way to check whether a user has administrative privileges. However, when I try to use boolean logic on this it fails to work. $user = [Security.Principal.WindowsIdentity]::GetCurrent(); (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) if($user = $false){cls;Write-warning "Starting and stopping services can only be done with administrative privileges.`nPlease restart this script from an elevated

Is there any way to get the list of functions from .cs file?

梦想的初衷 提交于 2019-12-12 21:02:05
问题 My manager asked me to get the list of functions available inside each .cs file in our project. But the number of .cs files are huge. It is tough to look by manual. Is there any way to find the function name in .cs files by writing script or code? 回答1: Use reflection. If all the files are in the project then you can use: Type[] types = Assembly.GetExecutingAssembly().GetTypes(); foreach (Type toCheck in toProcess) { MemberInfo[] memberInfos = toCheck.GetMembers(BindingFlags.NonPublic |

Set-ADGroup cannot find group just created in same script (PowerShell)

百般思念 提交于 2019-12-12 19:30:32
问题 I'm working on a script to create Security and Distribution groups so that they will follow our naming standards. In the script I use New-DistributionGroup to create the group, Set-DistributionGroup to add some Custom attributes, and then want to use Set-ADGroup to add a Description. (aside: what a pain that New-DistributionGroup can't do these things!) Despite setting sleep times up to 30 seconds, Set-ADGroup always fails with: Set-ADGroup : Cannot find an object with identity: '

Real Time Data With Powershell GUI

喜欢而已 提交于 2019-12-12 17:38:46
问题 I'm struggling here. Using Powershell and GUI, how to automatically refresh data on a form? Example with the script below, how to automatically update the label with the number of process executed by my computer? Add-Type -AssemblyName System.Windows.Forms $Form = New-Object system.Windows.Forms.Form $Form.Text = "Sample Form" $Label = New-Object System.Windows.Forms.Label $Label.Text = "Number of process executed on my computer" $Label.AutoSize = $True $Form.Controls.Add($Label) $Form

Why does “F5 - Start Debugging” ignore breakpoints in PowerGUI?

て烟熏妆下的殇ゞ 提交于 2019-12-12 16:09:47
问题 I'm not sure if I'm being a bit thick, but I have a simple script with three lines: $iis = 90000 $name = "somesite" Write-Host("Values are: $iis and $name") If I set a breakpoint on any of these lines and hit the play button (F5 - start debugging) the script runs but the breakpoints are ignored. If I start the script with F11 (step into) I can step through just fine, however hitting F5 to run to the next breakpoint again causes PowerGUI to ignore the breakpoints The script I'm working on is

Emulating a class with methods in a module

 ̄綄美尐妖づ 提交于 2019-12-12 13:39:00
问题 I am using PowerShell 2.0 (can't upgrade) and I am writing a series of scripts that uses some information from Active Directory. Coming from an OOP languages like C++, I want to emulate a class in PowerShell 2.0, but I know that they only have the class statement in 5.0, and I don't want to use C# to embed classes because I already have a functions (which will be my methods) written in Powershell.. I read this: Powershell. Create a class file to hold Custom Objects? And I am able to do a

Create a folder alias in PowerShell

隐身守侯 提交于 2019-12-12 12:18:29
问题 I know that I can create variable that represents a folder path in my profile. For example, $here = Split-Path -Parent $MyInvocation.MyCommand.Path Is there an easy way to create an alias to a directory in PowerShell? Create an alias PS> Create-FolderAlias -name $foo -path "C:\Program Files" Create an alias based on another alias PS> Create-FolderAlias -name $bar -path $foo + "\Microsoft" Use alias as expected PS> cd $foo It would be nice if these aliases would be persisted between sessions.

Best way to check if an PowerShell Object exist?

早过忘川 提交于 2019-12-12 09:28:27
问题 I am looking for the best way to check if a Com Object exists. Here is the code that I have; I'd like to improve the last line: $ie = New-Object -ComObject InternetExplorer.Application $ie.Navigate("http://www.stackoverflow.com") $ie.Visible = $true $ie -ne $null #Are there better options? 回答1: I would stick with the $null check since any value other than '' (empty string), 0 , $false and $null will pass the check: if ($ie) {...} . 回答2: You can also do if ($ie) { # Do Something if $ie is not

SendMessage is causing script to hang

流过昼夜 提交于 2019-12-12 07:19:17
问题 I am having an issue where the SendMessage() function is causing a script to hang and thus never exiting though it is running the SendMessage like it should (Its task completes). Is there anyway to get around this because I am having a heck of a time killing it from the master script. Stop-job -name offmon or Remove-job -name offmon -force will not kill it. With out the force on remove-job it reports it cannot kill it because it is not finished. I need to call this many times per day and each

How to iterate through remote registry keys using powershell?

断了今生、忘了曾经 提交于 2019-12-12 06:39:02
问题 I have a powershell script that goes through a list of name value pairs in a registry key and do some maniuplations. All of this happens inside the below foreach-object loop Get-ChildItem "HKLM:\SOFTWARE\PathA\pathB" -Recurse | ForEach-Object { $regkey = (Get-ItemProperty $_.PSPath) | Where-Object { $_.PSPath -match 'debug' } if ($masterList.Contains($_.Name)) #Check if the reg key is in master list { Set-ItemProperty -Path $regkey.PSPath -Name $_.Name -Value 1 } } This works perfectly fine