powershell-module

Call a specific version of PowerShell from C#

£可爱£侵袭症+ 提交于 2019-12-02 07:49:54
I'm trying to use the Get-VM Cmdlet called from C# on a Hyper-V host. Obviously, the according PowerShell module Hyper-V has to be imported first. The import fails, however - apparently because the module is supported only on PowerShell 3.0 (at least that's what I figure from this article ). The PowerShell used by System.Management.Automation seems to be version 2.0, though. InitialSessionState iss = InitialSessionState.CreateDefault(); iss.ImportPSModule(new string[] { "Hyper-V" }); Runspace runSpace = RunspaceFactory.CreateRunspace(iss); runSpace.Open(); foreach (var err in (ArrayList

Function does not get binding when used in Module

。_饼干妹妹 提交于 2019-12-01 09:24:29
I'm trying to put this function : function Test-Any { [CmdletBinding()] param($EvaluateCondition, [Parameter(ValueFromPipeline = $true)] $ObjectToTest) begin { $any = $false } process { if (-not $any -and (& $EvaluateCondition $ObjectToTest)) { $any = $true } } end { $any } } into a module. I just created a new module, the my-scripts.psm1 file, which contains just the above function and import it with Import-Module <absolute path> . The problem is that if I use the function from the module 1..4 | Test-Any { $_ -gt 3 } returns false, because $_ is not set to the value from the pipe. If I define

Is there any way for a powershell module to get at its caller's scope?

对着背影说爱祢 提交于 2019-12-01 06:39:50
I have a collection of utility functions and other code that I dot-source into every powershell file I write. Having gotten bit by caller scope variables influencing it, I started looking into changing it into a powershell module. I ran into problems with a couple of the special things I do in it where I actually do want some interaction between the scopes. I'm wondering if there is anyway to "get at" the scope of a module's caller to keep this functionality while moving to a powershell module? If not, is my best path forward to keep these more specialized things in a dot-sourced file and move

Is there any way for a powershell module to get at its caller's scope?

倖福魔咒の 提交于 2019-12-01 06:06:22
问题 I have a collection of utility functions and other code that I dot-source into every powershell file I write. Having gotten bit by caller scope variables influencing it, I started looking into changing it into a powershell module. I ran into problems with a couple of the special things I do in it where I actually do want some interaction between the scopes. I'm wondering if there is anyway to "get at" the scope of a module's caller to keep this functionality while moving to a powershell

Function does not get binding when used in Module

左心房为你撑大大i 提交于 2019-12-01 05:43:44
问题 I'm trying to put this function: function Test-Any { [CmdletBinding()] param($EvaluateCondition, [Parameter(ValueFromPipeline = $true)] $ObjectToTest) begin { $any = $false } process { if (-not $any -and (& $EvaluateCondition $ObjectToTest)) { $any = $true } } end { $any } } into a module. I just created a new module, the my-scripts.psm1 file, which contains just the above function and import it with Import-Module <absolute path> . The problem is that if I use the function from the module 1.

PowerShell Module Manifest - automatically loading a Required Module

跟風遠走 提交于 2019-11-30 19:46:24
I am creating a module that depends on several other modules that I need loaded into the global environment. I tried creating a script and using ScriptsToProcess to import the modules, but it looks like the check for RequiredModules happens before ScriptstoProcess runs. Is there a nice, clean way in a Module Manifest to both Require a module and load it automatically if its not already loaded? If the module could not be loaded, then the RequiredModule would throw an error. Jaykul In PowerShell 3 and up, RequiredModules are automatically loaded It's also the only way to make sure people using

PowerShell Module Manifest - automatically loading a Required Module

有些话、适合烂在心里 提交于 2019-11-30 04:05:01
问题 I am creating a module that depends on several other modules that I need loaded into the global environment. I tried creating a script and using ScriptsToProcess to import the modules, but it looks like the check for RequiredModules happens before ScriptstoProcess runs. Is there a nice, clean way in a Module Manifest to both Require a module and load it automatically if its not already loaded? If the module could not be loaded, then the RequiredModule would throw an error. 回答1: In PowerShell

Powershell Module Design - Export-ModuleMember

纵然是瞬间 提交于 2019-11-29 10:27:04
I am building a module that exports a cmdlet that I would like to make available through my profile. The implementation of this cmdlet is spread across multiple implementation files that contain implementation functions I don't want to make publicly available. So I use Export-ModuleMember to hide them. get_something.psm1 import-module .\get_something_impl.psm1 function Get-Something { [cmdletbinding()] Get-SomethingImplementation } Export-ModuleMember -Function Get-Something I then add get_something.psm1 to my profile. By exporting only Get-Something all of my implementation functions remain

Powershell Module Design - Export-ModuleMember

懵懂的女人 提交于 2019-11-28 03:40:40
问题 I am building a module that exports a cmdlet that I would like to make available through my profile. The implementation of this cmdlet is spread across multiple implementation files that contain implementation functions I don't want to make publicly available. So I use Export-ModuleMember to hide them. get_something.psm1 import-module .\get_something_impl.psm1 function Get-Something { [cmdletbinding()] Get-SomethingImplementation } Export-ModuleMember -Function Get-Something I then add get

How to execute .sql file using powershell?

白昼怎懂夜的黑 提交于 2019-11-27 03:03:19
I have a . sql file. I am trying to pass connection string details through a Powershell script and invoke an .sql file. I was searching and came up with a cmdlet related to Invoke-sqlcmd . While I was trying to find a module corresponding to SQL, I did not find any one in my machine. Should I install anything in my machine (the machine already has SQL Server Management Studio 2008 R2) to get the modules or is there any easy way to execute .sql files using Powershell? CB. Try to see if SQL snap-ins are present: get-pssnapin -Registered Name : SqlServerCmdletSnapin100 PSVersion : 2.0 Description