Scripting Question

Ask your Windows 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.
boskap
Posts: 21
Joined: Sun Jul 15, 2018 4:00 pm

Scripting Question

Post by boskap » Thu Sep 20, 2018 7:37 am

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

User avatar
jvierra
Posts: 13681
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Scripting Question

Post by jvierra » Thu Sep 20, 2018 10:32 am

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
Joined: Sun Jul 15, 2018 4:00 pm

Re: Scripting Question

Post by boskap » Thu Sep 20, 2018 12:58 pm

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

User avatar
jvierra
Posts: 13681
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Scripting Question

Post by jvierra » Thu Sep 20, 2018 1:05 pm

help join-path -full

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

User avatar
jvierra
Posts: 13681
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Scripting Question

Post by jvierra » Thu Sep 20, 2018 1:08 pm

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
Joined: Sun Jul 15, 2018 4:00 pm

Re: Scripting Question

Post by boskap » Thu Sep 20, 2018 2:29 pm

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

User avatar
jvierra
Posts: 13681
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Scripting Question

Post by jvierra » Thu Sep 20, 2018 3:00 pm

No. PS requires an installed PS engine version.

boskap
Posts: 21
Joined: Sun Jul 15, 2018 4:00 pm

Re: Scripting Question

Post by boskap » Tue Oct 02, 2018 3:00 pm

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

User avatar
jvierra
Posts: 13681
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Scripting Question

Post by jvierra » Tue Oct 02, 2018 3:56 pm

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
Joined: Sun Jul 15, 2018 4:00 pm

Re: Scripting Question

Post by boskap » Wed Oct 03, 2018 1:24 pm

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

}
}

Locked