powershell-module

ValidateScript ParameterAttribute in C# Binary PowerShell Module

风格不统一 提交于 2019-12-24 13:09:16
问题 I have recently begun experimenting with Binary PowerShell Programming in C#, and I am having some trouble with ParameterValidationAttributes, the ValidateScript Attribute mostly. Basically, i want to create a Param named "ComputerName" and validate the computer is online at that time. It was easy in PowerShell: [Parameter(ValueFromPipeLine = $true)] [ValidateScript({ if (Test-Connection -ComputerName $_ -Quiet -Count 1) { $true } else { throw "Unable to connect to $_." }})] [String]

Handle loading of .net module into PowerShell

为君一笑 提交于 2019-12-23 05:49:33
问题 I have an .NET assembly that will get imported by a Powershell session using: Import-Module MyAssemblyFirst.dll What I need is to automatically load another custom assembly ( MyAssemblySecond.dll ) that I know about when MyAssemblyFirst.dll gets imported. The scenario I am looking for is: Import-Module MyAssemblyFirst.dll Import-Module MyAssemblySecond.dll but I would rather like to have only one Import-Module call: Import-Module MyAssemblyFirst.dll ...and somehow to trigger loading the

Where can I put Powershell modules that will be accessed by multiple users?

允我心安 提交于 2019-12-19 11:53:44
问题 I used the variable $Env:PSModulePath , It provided two paths. > C:\Users\My User\Documents\WindowsPowerShell\Modules > C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ Seems like My User will be accessible only for me. But my module will be used by multiple person and also when uninstalling my application [I am installing my module via msi], even if some other user uninstalls it should be removed. Can I use "C:\Windows\system32\WindowsPowerShell\v1.0\Modules\" for my application related

Does #Requires work in module scripts?

丶灬走出姿态 提交于 2019-12-08 20:05:07
问题 Consider the following module script: MyWebApp.psm1 : #Requires -Version 4 #Requires -Modules WebAdministration function Test-MyWebApp() { return ((Get-WebApplication 'myapp') -ne $null) } ( Export-ModuleMember omitted for simplicity. The script still works as a module without it.) If this were a ps1 script, the #Requires comments at the top would force PowerShell to throw an error if The version were lower than 4 The WebAdministration module could not be loaded (imported) But if I try to

PowerShell 5.1 - How to uninstall module which is currently use

久未见 提交于 2019-12-08 14:44:55
问题 We are using some PowerShell modules in one deployment PowerShell script. Using following command we are installing module (i.e. XXXX) into "C:\Program Files\WindowsPowerShell\Modules". Install-Module -Name "XXXX" -AllowClobber -RequiredVersion "XXXX" -Repository "XXXX" -Scope AllUsers Now once we used the functionality of this module, we uninstall it at the end of deployment script using following command. Remove-Module -Name "XXXX" -force Uninstall-Module -Name "XXXX" -AllVersions -force

How can I reload modules from one open work environment to affect another working environment

坚强是说给别人听的谎言 提交于 2019-12-08 07:26:09
问题 I have my PowerShell project broken into modules. But because they are modules I have to reload them every time I change them. So I wrote a loop that has a FileSystemWatcher and if one of the .psm1 file changes it will either reload or import that module. The problem is that the above loop isn't going to let me run other scripts in its working environment, so a new environment will not have the same modules loaded/reloaded for it. I need to keep these modules out of the primary default

$_ variable used in function from a module is empty (PowerShell)

青春壹個敷衍的年華 提交于 2019-12-06 18:31:37
问题 One question for you is here ;) I have this function: function Set-DbFile { param( [Parameter(ValueFromPipeline=$true)] [System.IO.FileInfo[]] $InputObject, [Parameter(ValueFromPipelineByPropertyName=$true)] [scriptblock] $Properties ) process { $InputObject | % { Write-Host `nInside. Storing $_.Name $props = & $Properties Write-Host ' properties for the file are: ' -nonew write-Host ($props.GetEnumerator()| %{"{0}-{1}" -f $_.key,$_.Value}) } } } Look at the $Properties . It should be

$_ variable used in function from a module is empty (PowerShell)

女生的网名这么多〃 提交于 2019-12-05 01:21:12
One question for you is here ;) I have this function: function Set-DbFile { param( [Parameter(ValueFromPipeline=$true)] [System.IO.FileInfo[]] $InputObject, [Parameter(ValueFromPipelineByPropertyName=$true)] [scriptblock] $Properties ) process { $InputObject | % { Write-Host `nInside. Storing $_.Name $props = & $Properties Write-Host ' properties for the file are: ' -nonew write-Host ($props.GetEnumerator()| %{"{0}-{1}" -f $_.key,$_.Value}) } } } Look at the $Properties . It should be evaluated for each file and then the file and the properties should be processed further. Example how to use

Powershell Remoting: using imported module cmdlets in a remote pssession

时间秒杀一切 提交于 2019-12-03 11:49:06
问题 Is there a way to use modules that were imported in a local session in a remote session? I looked at import-pssession, but I don't know how to get the local session. Here's a sample of what I want to do. import-module .\MyModule\MyModule.ps1 $session = new-pssession -computerName RemoteComputer invoke-command -session $session -scriptblock { Use-CmdletFromMyModule } Also, I do not want to import-module in the remote session, as the ps1 files are not on that server. 回答1: I ended up hacking

Installing PowerShell module persistently for all users

痞子三分冷 提交于 2019-12-02 20:57:24
I'm installing a PowerShell module via Octopus Deploy onto a number of different servers. For testing purposes, I went with the guidance of Microsoft's documentation for installing PowerShell Modules . This worked fine, but as the documentation stated, my changes would be visible only for the current session. That is, if I were to do the following: $modulePath = [Environment]::GetEnvironmentVariable("PSModulePath", [EnvironmentVariableTarget]::Machine) # More practically, this would be some logic to install only if not present $modulePath += ";C:\CustomModules" [Environment]: