Page 1 of 2

Scripting Question

Posted: Thu Sep 20, 2018 7:37 am
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

Re: Scripting Question

Posted: Thu Sep 20, 2018 10:32 am
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".

Re: Scripting Question

Posted: Thu Sep 20, 2018 12:58 pm
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

Re: Scripting Question

Posted: Thu Sep 20, 2018 1:05 pm
by jvierra
help join-path -full

"compiled exe"? Do you mean a "packaged" script using a Sapien product?

Re: Scripting Question

Posted: Thu Sep 20, 2018 1:08 pm
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

Re: Scripting Question

Posted: Thu Sep 20, 2018 2:29 pm
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

Re: Scripting Question

Posted: Thu Sep 20, 2018 3:00 pm
by jvierra
No. PS requires an installed PS engine version.

Re: Scripting Question

Posted: Tue Oct 02, 2018 3:00 pm
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

Re: Scripting Question

Posted: Tue Oct 02, 2018 3:56 pm
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.

Re: Scripting Question

Posted: Wed Oct 03, 2018 1:24 pm
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

}
}