powershell-5.0

Export Powershell 5 enum declaration from a Module

断了今生、忘了曾经 提交于 2020-01-02 02:40:30
问题 I have an enum type defined within a module. How do I export it to be accessible from outside once the module has been loaded? enum fruits { apple pie } function new-fruit { Param( [fruits]$myfruit ) write-host $myfruit } My advanced function takes the enum instead of the ValidateSet which works if the enum is available, but fails if it isn't. Update: Separating it into a ps1 and dot-sourcing it (ScriptsToProcess) works, however I would wish that there's a cleaner way. 回答1: You can access the

How do I force powershell to reload a custom module?

只谈情不闲聊 提交于 2020-01-01 22:47:50
问题 I have created a module 'ActiveDirectory.psm1' which contains a class in powershellv5. I am importing that module in another file called 'test.ps1' and then calling a method from the class. test.ps1 contains the following: using module '\\ser01\Shared\Scripts\Windows Powershell\modules\ActiveDirectory\ActiveDirectory.psm1' Set-StrictMode -version Latest; $AD = [ActiveDirectory]::New('CS'); $AD.SyncGroupMembership($True); It all works as expected BUT when I make a change to ActiveDirectory

Set-Content appends a newline (line break, CRLF) at the end of my file

徘徊边缘 提交于 2019-12-28 02:08:09
问题 My original config file (web1.config) has no extra line and when viewed in notepad (showing all characters) looks as: <?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.6" /> <httpRuntime targetFramework="4.6" /> </system.web> <appSettings> <add key="myConnectionString" value="server=localhost;database=myDb;uid=myUser;password=myPass;" /> </appSettings> </configuration> Now, I need to apply the script to change my database name to something else

Missing Help Files in Powershell V5.0

家住魔仙堡 提交于 2019-12-23 21:58:39
问题 Powershell Version: 5.0.10586.494 I just began working with powershell this weekend and I discovered that I cannot find any help files when using the shell, for example, I was looking to read the about_Comparison_Operators help file but it seems as though the console cannot find it. When doing: Get-Help About_* the only result I get is About_CimSession ... it seems like there are no other help files? This TechNet article suggests that in Powershell v3 the module must be imported, To download

Return array with single element in PowerShell

六月ゝ 毕业季﹏ 提交于 2019-12-23 15:43:28
问题 This should be very simple - I need to return an array of hashtables from a function. This works when there is more than one hashtable, but when there is only one then the result is not an array. I'd rather not test if the result is an array or not. function GetArrayWith1Hashtable() { $array = @() $hashtable = @{} $hashtable["a"] = "a" $hashtable["b"] = "b" $hashtable["c"] = "c" $array += $hashtable Write-Host "GetArrayWith1Hashtable array.Length =" $array.Length Write-Host

Powershell version 5 Copy-Item -FromSession cannot be found

夙愿已清 提交于 2019-12-23 15:28:40
问题 I'm trying to copy some log files from a remote session via the -FromSession paramter of the Copy-Item cmdlet. On the calling machine I've PS version 5 installed. When running the script I get following error: Copy-Item : A parameter cannot be found that matches parameter name 'FromSession'. When I'm calling $PSVersionTable on the source machine I get following output: PSVersion 5.0.10586.672 PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.10586.672 CLRVersion 4.0.30319.42000

Get great grand parent folder path (three levels up) in PowerShell?

一曲冷凌霜 提交于 2019-12-23 12:24:19
问题 Is there an elegant way to get the (great-grandparent folder) three levels up from a folder path? I'm only looking to get C:\folderA\folderB from the full path, but both solutions just seem ugly to me. $path = "C:\folderA\folderB\folderC\FolderD\folderE" # option 1 (Get-Item $path).parent.parent.parent.FullName # option 2 $path | Split-Path -Parent | Split-Path -Parent | Split-Path -Parent 回答1: Try this (works only if the path exists): (Get-Item "$path\..\..\..").FullName Alternatively, if

The PowerShell ISE sometimes behaves unpredictably after code changes are made

牧云@^-^@ 提交于 2019-12-22 07:13:27
问题 I'm using the PowerShell ISE (PS version 5.0). If I run this code: Write-Host "This" It outputs: This If I modify the script like this: Write-Host "That" It outputs: That Great. As expected. Now, if I have this code: $Form = New-Object System.Windows.Forms.Form $Timer = New-Object System.Windows.Forms.Timer $Timer.Add_Tick( { &{ Write-Output "Here" $Form.Close()} | Write-Host }) $Timer.Interval = 3000 $Timer.start() $result = $Form.ShowDialog() It outputs: Here If I change anything in the

Powershell Copy Files with a Blacklist (Exclude) and a Whitelist (Include)

若如初见. 提交于 2019-12-21 05:35:46
问题 I'm translating some msbuild scripts to powershell. In msbuild, I can generate a blacklist and/or whitelist of files I want to (recursively) copy to a destination folder. As seen below: <?xml version="1.0" encoding="utf-8"?> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapped"> <PropertyGroup> <!-- Always declare some kind of "base directory" and then work off of that in the majority of cases --> <WorkingCheckout>.</WorkingCheckout>

Trying to mix data from a CSV and a hashtable to make a variable

妖精的绣舞 提交于 2019-12-20 03:23:51
问题 I stopped over at Code Review, asking how I could streamline a script and was advised to use a hashtable as it would clean up the code. I was given a very basic example but it wasn't plug-and-play . I've worked up some basic code but it's not doing what I think it should. Knowing the Code Review folks aren't there for support like this, here i am, looking for help with combining a variable from a CSV and a hashtable. I'll leave sample data from my CSV and the Powershell code below. Sample CSV