powershell-2.0

Create a manifest for nested PowerShell modules

試著忘記壹切 提交于 2019-12-04 22:22:07
问题 I'm having a minor issue with nested, PowerShell modules. Get-Module correctly identifies the ExportedCommands , but the ModuleType is listed as Script , rather than Manifest : PS>get-module ModuleType Name ExportedCommands ---------- ---- ---------------- Script Bar Get-Bar Script Foo Get-Foo Directory structure: |-Modules |-Foobar |-Foobar.psd1 |-Bar |-Bar.psm1 |-Foo |-Foo.psm1 Foobar.psd1: ... # Script module or binary module file associated with this manifest ModuleToProcess = '' #

Best way to check if an PowerShell Object exist?

爷,独闯天下 提交于 2019-12-04 22:11:29
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? I would stick with the $null check since any value other than '' (empty string), 0 , $false and $null will pass the check: if ($ie) {...} . ravikanth You can also do if ($ie) { # Do Something if $ie is not null } In your particular example perhaps you do not have to perform any checks at all . Is that

How to capture a Powershell CmdLet's verbose output when the CmdLet is programmatically Invoked from C#

*爱你&永不变心* 提交于 2019-12-04 19:11:19
问题 BACKGROUND I am using Powershell 2.0 on Windows 7. I am writing a cmdlet in a Powershell module ("module" is new to Powershell 2.0). To test the cmdlet I am writing Unit tests in Visual Studio 2008 that programmatically invoke the cmdlet. REFERENCE This Article on MSDN called "How to Invoke a Cmdlet from Within a Cmdlet" shows how to call a cmdlet from C#. THE SOURCE CODE This is a distilled version of my actual code — I've made it as small as possible so that you can see the problem I am

Powershell start-job -scriptblock cannot recognize the function defined in the same file?

孤者浪人 提交于 2019-12-04 18:14:31
问题 I have the following code. function createZip { Param ([String]$source, [String]$zipfile) Process { echo "zip: $source`n --> $zipfile" } } try { Start-Job -ScriptBlock { createZip "abd" "acd" } } catch { $_ | fl * -force } Get-Job | Wait-Job Get-Job | receive-job Get-Job | Remove-Job However, the script returns the following error. Id Name State HasMoreData Location Command -- ---- ----- ----------- -------- ------- 309 Job309 Running True localhost createZip "a... 309 Job309 Failed False

How to verify whether the share has write access?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 16:59:10
I am having share with write access. I am writing a powershell script to write a log file in that share. I would like to check the condition whether i am having write access to this share before writing in it. How to check for write access/Full control using powershell? I have tried with Get-ACL cmdlet. $Sharing= GEt-ACL "\\Myshare\foldername If ($Sharing.IsReadOnly) { "REadonly access" , you can't write" } It has Isreadonly property, But is there any way to ensure that the user has Fullcontrol access? This does the same thing as @Christian's C# just without compiling C#. function Test-Write {

Stop Powershell from exiting

让人想犯罪 __ 提交于 2019-12-04 16:04:53
问题 I know that there is that little -noexit switch for PowerShell. Is there anyway of staying in the shell without using that switch? In other words, I want a script command(s) that executes then leaves the shell open. 回答1: This script will not exit if you run it without arguments, e.g. by double-clicking on it: param($Work) # restart PowerShell with -noexit, the same script, and 1 if (!$Work) { powershell -noexit -file $MyInvocation.MyCommand.Path 1 return } # now the script does something #

Detect If IPv6 is Enabled on Windows Machines

蹲街弑〆低调 提交于 2019-12-04 15:42:35
I am writing a powershell script that will act as a build compliance test for our servers. One of the things I need to do is detect if IPv6 networking has been disabled. WMI indicates that this information can be found in the IPAddress Property of Win32_NetworkAdapterConfiguration but can be both IPv6 or IPv4. This does not give me a "yes/no" answer I am hoping to find. Other caveats are that I would prefer not to scrape the details by accessing the registry directly, nor scrape from the output of a command such as ipconfig. Given our environment has a mix of 2003/2008 machines, can anyone

Checking for a range in Powershell

試著忘記壹切 提交于 2019-12-04 15:35:06
I'm trying to write a script that will get an IP address of a computer and check to see whether it falls in a specific range of IPs. So for example, if the IP of the machine is 192.168.0.5, the script will check to see if it falls between the range 192.168.0.10 to 192.168.0.20. So far, my script is only able to get IPs of remote machines, but I just can't figure out how I would check if the IP is in a specific range. I'd appreciate any suggestions. Thank you. It might be easiest to let .NET do the work - there's an IPAddress class that can parse them to their numeric values for comparison.

Find and replace in files fails

ぃ、小莉子 提交于 2019-12-04 15:16:21
问题 I am trying to do find and replace in a file using following approach. Function Find-Replace ($FileFullpath, $FindString, $ReplacementString) { Get-Content $FileFullpath | Foreach-Object {$_ -replace $FindString, $ReplacementString } | Set-Content $FileFullpath } Find-Replace "c:\program files (x86)\MyProj\web.config" $OldServiceName $NewServiceName But i am always getting error. Set-Content : The process cannot access the file 'c:\program files (x86)\MyProj\web.config' because it is being

Referencing System.Management.Automation.dll in a correct, multi-OS friendly way

送分小仙女□ 提交于 2019-12-04 13:18:46
I am writing a piece of C# that will invoke PS scripts. As part of this I’m referencing System.Management.Automation.dll, the library that the PS guys expose that makes my life easier. I assume this is the right library to use (internet searching seems to confirm but if I’m wrong let me know). This library is installed as part of the PS install / Windows SDK. I’m finding two weird things about this library: I always have to reference it with a full path (even though it’s in the GAC?) The path is different on different OSs I’ve found it in the following paths (ignoring GAC paths which I assume