Scripting Question

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
boskap
Posts: 21
Last visit: Sun Oct 21, 2018 4:24 am

Scripting Question

Post by boskap »

hello
Sorry my english is not so good

i have a question / problem with the browse folder function
when i choose a folder and set a variable with this folder , adding a subfolder the path is ok

$Back_Rec_Path = $folderbrowserdialog2.SelectedPath
$global:DestPath = $Back_Rec_Path + "\BACKUP\" + $Env:USERNAME # zb.: C:\Backup\Username...

the $destpath variabe is then ok, like d:\test\backup\

but when i choose a root drive like d:\ the $destpath is d:\\backup
now i have to backslash

what i make wrong

a solution was the following
The following is a safe way to join multiple path segments.

$global:DestPath = [system.io.path]::Combine($Back_Rec_Path.'BACKUP',$Env:USERNAME)
But this only works with powershell 5
it not working with v2 what is most installed on our win 7 clients

how i mus t change the script to run in v2
thx
Peter
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Scripting Question

Post by jvierra »

You should not be running V2. It is not considered safe and is mostly no longer supported.

You can also use "Join-Path".
boskap
Posts: 21
Last visit: Sun Oct 21, 2018 4:24 am

Re: Scripting Question

Post by boskap »

i no that i should not use it
but i have no choice
join-path
have you got an example

or is it possible to include v5 runtime in an compiled exe
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Scripting Question

Post by jvierra »

help join-path -full

"compiled exe"? Do you mean a "packaged" script using a Sapien product?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Scripting Question

Post by jvierra »

Note that [system.io.path]::Combine is a PS2/Net2 method. It works in ALL versions of PwoerShell.

See: https://docs.microsoft.com/en-us/dotnet ... mework-2.0
boskap
Posts: 21
Last visit: Sun Oct 21, 2018 4:24 am

Re: Scripting Question

Post by boskap »

ok i try i again

what i mean with runtime
is it possible to build an exe file with psv5 runtime is included
so it run also when on the target computer is no ps or an old version
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Scripting Question

Post by jvierra »

No. PS requires an installed PS engine version.
boskap
Posts: 21
Last visit: Sun Oct 21, 2018 4:24 am

Re: Scripting Question

Post by boskap »

sorry for my question but on machines with v2 ps that didnt work
$global:DestPath = [system.io.path]::Combine($Back_Rec_Path.'BACKUP',$Env:USERNAME)

the scrippt didnt create a folder with backup on that drive i select

when i switch to a machine with ps 5 all work
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Scripting Question

Post by jvierra »

The line you posted is not used for creating folders. It just creates a string. You have to use the "New-Item" command to create a folder.
boskap
Posts: 21
Last visit: Sun Oct 21, 2018 4:24 am

Re: Scripting Question

Post by boskap »

i know

$buttonBrowseFolder_Click = {
if ($folderbrowserdialog2.ShowDialog() -eq 'OK')
{
$textboxFolder.Text = $folderbrowserdialog2.SelectedPath
$Back_Rec_Path = $folderbrowserdialog2.SelectedPath
Update-ListBox $ListBox1 "create Backup Directory / read Backup Directory" -Append
$global:DestPath = [system.io.path]::Combine($Back_Rec_Path,'BACKUP', $Env:USERNAME)
Update-ListBox $ListBox1 "create Log Folder / read Log Folder" -Append
$global:DestPathLog = $DestPath + "\!LOG" #
$global:LogFile = $DestPathLog + "\Robocopy_Profil_copy.txt"

If (!(test-path $DestpathLog)) #sollte das Zielverzeichnis nicht vorhanden sein, läuft Robocopy wegen der LOG-Datei in einen Fehler...
{ New-Item -ItemType Directory -Force -Path $DestpathLog }
#$tabcontrol1.Enabled = $true

}
}
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked