Author Topic: Powershell problem with defining the path for a file pls help  (Read 1543 times)

0 Members and 1 Guest are viewing this topic.

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
Hi!
I'm stumbled into a small problem with my script and need help about to figure out how so solve my problem.

I have to start a 1.ps1 file from a .bat file which are on my usb stick.

Everything is working like a charm until I change my PC and put the usb drive to the second pc.
The problem is the path, the drive letter of the usb stick which can be different on different pc's.

Here is my code where the path is defined as "F:\X\1.ps1":

Code: [Select]
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""F:\X\1.ps1""' -Verb RunAs}"
How can I run the 1.ps1 file or how to declare the path in a way does it work automatically when not an F drive is
assigned to my usb stick?

There is no way to predict on what pc what drive letter will be assigned.
But the 1.ps1 file will always be in the X folder on the usb stick.

Thanks for any sugestion or help.
 

Offline AntiProtonBoy

  • Frequent Contributor
  • **
  • Posts: 991
  • Country: au
  • I think I passed the Voight-Kampff test.
Re: Powershell problem with defining the path for a file pls help
« Reply #1 on: March 17, 2021, 10:35:15 pm »
Quote
There is no way to predict on what pc what drive letter will be assigned.

What you can do is manually assign the drive letter to your usb stick, like Z:\

The next time you plug it in, Windows might recognise the device and reassign Z:\

A more robust solution is enumerate all the available drives and then select the one with a particular volume label, or if it has the path you want to access. See Get-PSDrive.
« Last Edit: March 17, 2021, 10:37:49 pm by AntiProtonBoy »
 

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
Re: Powershell problem with defining the path for a file pls help
« Reply #2 on: March 17, 2021, 10:54:05 pm »
Thank you m8 for your response.

I come also to a similar solution like you mentioned, but I was not sure and maybe there would be
another solution what I cant figure out.

But now, as you also say this will be hard to solve in a dynamical way.

I hate this in Powershell.
Such a framework and it cant handle such a simple task, what is simple to do in the old cmd...
 

Offline gmb42

  • Frequent Contributor
  • **
  • Posts: 335
  • Country: gb
Re: Powershell problem with defining the path for a file pls help
« Reply #3 on: March 18, 2021, 11:39:20 am »
How is this a PowerShell issue?  From what I can see it's the .bat file that's supplying the incorrect absolute path to the invocation of the PowerShellScript.

I'm assuming that the .bat and .ps1 file are on the same USB device.  You could either use a relative path for the script, e.g.

Code: [Select]
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "".\X\1.ps1""' -Verb RunAs}"
or you can use batch statements to extract the drive letter of the batch script itself and concat that to the PowerShell script path, e.g.

Code: [Select]
set ScriptPath=%~d0\X\1.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%ScriptPath%""' -Verb RunAs}"

The batch path extractors are listed here.

 
The following users thanked this post: newbrain, Chriss

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
Re: Powershell problem with defining the path for a file pls help
« Reply #4 on: March 18, 2021, 10:54:32 pm »
Thank you very much for this help.
It works like charm.

Can you pls help me to figure out how to do this:
How can I in pwsh combine two variables in a way
to use that result as a file name?

Something like this:
Var1 = input from the user
Write the var1 + ".txt" to disk.
I cant figure out how to combain the
part "var1+.txt" in pwsh so i get the
complete filename.

Lets say in my batch file I read the filename
in the set /p name="Input file name:"
sysinfo > %name%.txt
How can I achieve this in pwsh?

Or how to put together two sting in one variable?

Thank you.
 

Offline gmb42

  • Frequent Contributor
  • **
  • Posts: 335
  • Country: gb
Re: Powershell problem with defining the path for a file pls help
« Reply #5 on: March 19, 2021, 10:58:46 am »
PowerShell variables are prefixed by "$".  You can use the "+" operator to join strings although there are many other ways to do that.

Code: [Select]
> $a = "Hello"
> $b = "World"
> $a + $b
HelloWorld

> $a + " " + $b
Hello World

> "$a $b"
Hello World

> "My output: {0} {1}" -f $a, $b
My output: Hello World
 
The following users thanked this post: Chriss


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf