by vandreytrindade at 2012-11-26 06:11:57
Hi,by powerschill at 2012-11-26 06:17:45
I want to create a script that runs no matter where they are in the computer.
Example:
Computer 1
User have: C:\Scripts\Working\PSmain_script.ps1
Inside that script, there’s a command to run another script in the same directory ".\PSdir_script.ps1".
After testing the script, the user sends those two scripts files to another user by e-mail.
Computer 2
The other user recieves the mail then save the scripts to C:\Temp\ directory.
PS: This user has a PowerShell profile with the command "Set-Location C:" in it.
So… when he tries to run the script it gives an error telling that the ".\PSdir_script.ps1" wasn’t found, because there isn’t any file in the C:\ called "PSdir_script.ps1".
I’ve tried to use the "Get-Location", but it always gave me the "C:" path, no matter the source of the script.
Any 1?
I’m trying to use this solution to get my script working:
viewtopic.php?f=9&t=794
You can use the following code to retrieve the location of the script that is being executed:by vandreytrindade at 2012-11-26 06:27:36$ScriptDirectory = Split-Path $MyInvocation.MyCommand.Definition -Parent | Split-Path -parent
Hi, thanks for the quick response, but I get this error:by powerschill at 2012-11-26 06:30:38
"PS C:\Active Directory> $ScriptDirectory = Split-Path $MyInvocation.MyCommand.Definition -Parent | Split-Path -parent
Split-Path : Não é possÃvel associar o argumento ao parâmetro ‘Path’ porque ele é uma cadeia de caracteres vazia.
No linha:1 caractere:76
+ $ScriptDirectory = Split-Path $MyInvocation.MyCommand.Definition -Parent | Split …
+
~~~~~
+ CategoryInfo : InvalidData: (:PSObject) [Split-Path], Parameter BindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.SplitPathCommand
PS C:\Active Directory>"
Error in english: Split-Path : Is not possible to associate the argument to the parameter ‘Path’ because it is a null chain… or something like this =p
You have to put that code in the script that calls the other script. It won’t work from the command prompt.by vandreytrindade at 2012-11-26 06:43:44
I’ve created a script with the line you gave to me:by powerschill at 2012-11-26 06:51:43
Import-Module ActiveDirectory
$ScriptDirectory = Split-Path $MyInvocation.MyCommand.Definition -Parent | Split-Path -parent
$ScriptDirectory
pause
Opened Windows Explorer, went to the "Active Directory" directory and clicked twice in the test.ps1 file.
The result of the $ScriptDirectory variable is the "C:" and not the "C:\Active Directory" folder.
[quote]Opened Windows Explorer, went to the "Active Directory" directory and clicked twice in the test.ps1 file.[/quote]by vandreytrindade at 2012-11-26 07:31:07
You aren’t able to double-click a PowerShell script to execute it. Did you make any changes to your system to make this possible?
How I’m not enabled to do that?by vandreytrindade at 2012-11-26 07:35:57
I always use my scripts in that way…
I only choose to open in PowerShell all the ps1 files…
It worked!
What happened is that the command you gave me removed the directory that I wanted to be used: "C:\Active Directory"
"$ScriptDirectory = Split-Path $MyInvocation.MyCommand.Definition -Parent | Split-Path -parent"
So I removed that part and tested like that:
$ScriptDirectory = Split-Path $MyInvocation.MyCommand.Definition -Parent
$actualdir = $ScriptDirectory.Remove(0,2)
cd $actualdir
dir
Thank you so much