powershell-2.0

Load variables from another powershell script

你说的曾经没有我的故事 提交于 2019-11-28 22:18:21
I have several scripts that could be reusing variables so I'd like to isolate variables in their own Variables.ps1 script, i.e. $var1 = "1" $var2 = "2" I'm trying to load these variables then print them out in the Main.ps1 script like this: .\Variables.ps1 $var1 $var2 This works if I first run .\Variables.ps1 but not if I just run Main.ps1. My environment is PowerShell ISE. What am I doing wrong? zdan The variables declared in Variables.ps1 are at "Script Scope". That is you can not see them outside of the scope of the script that declares them. One way to bring the variables in Variables.ps1

How to run a Powershell script from the command line and pass a directory as a parameter

99封情书 提交于 2019-11-28 21:56:50
问题 My Powershell script, Foo.ps1: Function Foo($directory) { echo $directory } if ($args.Length -eq 0) { echo "Usage: Foo <directory>" } else { Foo($args[0]) } From the Windows console: powershell -command .\Foo.ps1 Results in: "The term '.\Foo.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again." This is despite Foo.ps1 being in the current directory

Powershell 2 copy-item which creates a folder if doesn't exist

江枫思渺然 提交于 2019-11-28 21:00:19
$from = "\\something\1 XLS\2010_04_22\*" $to = "c:\out\1 XLS\2010_04_22\" copy-item $from $to -Recurse This works if c:\out\1 XLS\2010_04_22\ does exist . Is it possible with a single command to create c:\out\1 XLS\2010_04_22\ if it doesn't exist? Shay Levy Yes, add the -Force parameter. copy-item $from $to -Recurse -Force In PowerShell 2.0, it is still not possible to get the Copy-Item cmdlet to create the destination folder, you'll need code like this: $destinationFolder = "C:\My Stuff\Subdir" if (!(Test-Path -path $destinationFolder)) {New-Item $destinationFolder -Type Directory} Copy-Item

How to write a custom powershell host [closed]

▼魔方 西西 提交于 2019-11-28 19:52:50
Similar to nuget Looking for any starter material hopefully before delving into the debugger MSDN has a section devoted to writing a PowerShell host in the PowerShell SDK documentation, which is a nice starting point. Besides that a search returns the following: http://powershellstation.com/2009/10/12/writing-your-own-powershell-hosting-app-part-1-introduction/ http://dougfinke.com/blog/index.php/2009/09/02/how-to-host-powershell-in-a-wpf-application/ There is no great documentation. I've learned most of what I know by experimentation and example. And reflection. Not the personal kind, the

How do you support PowerShell's -WhatIf & -Confirm parameters in a Cmdlet that calls other Cmdlets?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 19:22:46
I have a PowerShell script cmdlet that supports the -WhatIf & -Confirm parameters. It does this by calling the $PSCmdlet.ShouldProcess() method before performing the change. This works as expected. The problem I have is that my Cmdlet is implemented by calling other Cmdlets and the -WhatIf or -Confirm parameters are not passed along to the Cmdlets I invoke. How can I pass along the values of -WhatIf and -Confirm to the Cmdlets I call from my Cmdlet? For example, if my Cmdlet is Stop-CompanyXyzServices and it uses Stop-Service to implement its action. If -WhatIf is passed to Stop

How do I retrieve the available commands from a module?

谁都会走 提交于 2019-11-28 19:04:16
To know which PowerShell modules are available on a machine I use the command Get-Module -ListAvailable This returns a list with module-type, -name and the exported commands. But the exported commands are always empty and just displaying {} . Why is this not displayed? Do I have to use another parameter or is there another cmdlet or method to retrieve the available commands? Shay Levy Exported commands are not available if the module is not loaded. You need to load the module first and then execute Get-Command : Import-Module -Name <ModuleName> Get-Command -Module <ModuleName> user2095160 Use

Rename computer and join to domain in one step with PowerShell

ぃ、小莉子 提交于 2019-11-28 18:59:08
问题 Goal: On a computer running Windows Server 2008 R2, use PowerShell 2.0 to: Rename the computer Join the computer to a domain Condition: Steps 1 and 2 must be performed together, i.e., without a computer restart between them Functions I'm Using These are the PowerShell functions I've created for each step. Rename Computer According to my Internet research, PowerShell 2.0 at one point before release had a built-in cmdlet called Rename-Computer , but it was removed for reasons unknown in CTP 3.

PowerShell: Create Local User Account

亡梦爱人 提交于 2019-11-28 17:23:45
I need to create a new local user account, and then add them to the local Administrators group. Can this be done in PowerShell? EDIT: # Create new local Admin user for script purposes $Computer = [ADSI]"WinNT://$Env:COMPUTERNAME,Computer" $LocalAdmin = $Computer.Create("User", "LocalAdmin") $LocalAdmin.SetPassword("Password01") $LocalAdmin.SetInfo() $LocalAdmin.FullName = "Local Admin by Powershell" $LocalAdmin.SetInfo() $LocalAdmin.UserFlags = 64 + 65536 # ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD $LocalAdmin.SetInfo() I have this, but was wondering if there is anything more

How to import custom PowerShell module into the remote session?

烈酒焚心 提交于 2019-11-28 17:13:13
I'm developing a custom PowerShell module, which I'd like to use in context of a remote session to a different computer. The following code (which obviously doesn't work) explains what I'm trying to achieve: import-module .\MyCustomModule.psm1 $session = new-pssession -computerName server01 invoke-command -session $session -scriptblock { <# use function defined in MyCustomModule here #> } The first question is whether it is at all possible to achieve this scenario? I mean I would only like my custom module to be physically present on my machine, not on remote server. I have found this thread ,

How to open Powershell Console Window from Powershell

拟墨画扇 提交于 2019-11-28 16:57:58
问题 I am writing a script to use multiple plink (PuTTY) sessions as a Windows version of clusterssh. I am stuck however because I want to open multiple Powershell windows from powershell. When I type the command for powershell, it opens a new session. This is similar to typing bash in bash. I want multiple physical windows opening. I tried -windowstyle as well as the other args to no avail. I was wondering if there is a way you know of. I really appreciate your help. I looked and didn't find