natalie.solero
Goto Top

Powershell Folder Picker Dialog

Hello Guys face-smile

i'm just thinking about to implement a folder picking dialog box in my existing script.
In my existing script i want to install fonts on a remote machine. This script is working fine, but i need to write the folder path harcoded into the script.
Now i want to change this.

This is my script which is installing the fonts.
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
if (!$myWindowsPrincipal.IsInRole($adminRole)){
    start-process "powershell" -Verb "runas" -ArgumentList "-File",$MyInvocation.MyCommand.Definition  
    exit
}
$name = read-host "give hostname"  
PsExec.exe \\$Name -s winrm.cmd quickconfig -q
#Computers to install fonts to
$ComputerArray = @($Name)
#A Share containing only the fonts you want to install
$FontDir="C:\temp"  
#Wil be created on remote Pc if not exists, fonts will be copied here and deleted after install.
$PcStagingDir="c:\fonts"  
 
foreach ($pc in $ComputerArray) {
If((Test-Connection -ComputerName  $pc -Count  1 -ErrorAction SilentlyContinue) -and ($Winrmstatus=Test-WSMan -ComputerName $pc -ErrorAction SilentlyContinue)) {
$RemotePcStagingDir = "\\$pc\$($PcStagingDir.replace(':','$'))"  
$DisabledSvcs=@()
$ServiceName = @("WinRM")  
foreach ($svc in $ServiceName) {
        $i=0
        while(((Get-Service -ComputerName $pc -Name $svc -ErrorAction SilentlyContinue).status -ne "Running")-and($i -lt 10)){  
            if((Get-Service -ComputerName $pc -Name $svc -ErrorAction SilentlyContinue).StartType -eq "Disabled"){  
            Write-Host "Try $i , Setting service $svc StartType to Manual on $pc ..."  
            $DisabledSvcs+=$svc
            Set-Service -ComputerName $pc -Name $svc -StartupType Manual -ErrorAction SilentlyContinue}
        Write-Host "Try $i / 10 , Starting $svc Service on $pc ..."  
        $commandz="sc \\"+$pc +" Start "+$svc  
        & cmd.exe /c $commandz | Out-Null
        $i++
        sleep 3}
        if($i -ge 10){break}
    }
if($i -ge 10){Write-Host "Could NOT start service $svc, Skipping Computer $pc" -ForegroundColor Red}else{  
if ( -not (Test-Path -Path $RemotePcStagingDir)){New-Item -Path $RemotePcStagingDir -ItemType Directory -Force}
$RemoteWinDir=Invoke-Command -ComputerName $pc -ScriptBlock {return $env:windir}
foreach($FontFile in (Get-ChildItem -file -path $FontDir)){
    if(-not(Test-Path "\\$pc\$($RemoteWinDir.replace(':','$'))\Fonts\$FontFile")){  
        Copy-Item "$FontDir\$FontFile" -Destination $RemotePcStagingDir -Force  
        Invoke-Command -ComputerName $pc -ScriptBlock {
       $filePath="$using:PcStagingDir\$using:FontFile"  
       $fontRegistryPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"  
       $fontsFolderPath = "$($env:windir)\fonts"  
    # Create hashtable containing valid font file extensions and text to append to Registry entry name.
    $hashFontFileTypes = @{}
    $hashFontFileTypes.Add(".fon", "")  
    $hashFontFileTypes.Add(".fnt", "")  
    $hashFontFileTypes.Add(".ttf", " (TrueType)")  
    $hashFontFileTypes.Add(".ttc", " (TrueType)")  
    $hashFontFileTypes.Add(".otf", " (OpenType)")  
    try
    {
        [string]$filePath = (Get-Item $filePath).FullName
        [string]$fileDir  = split-path $filePath
        [string]$fileName = split-path $filePath -leaf
        [string]$fileExt = (Get-Item $filePath).extension
        [string]$fileBaseName = $fileName -replace($fileExt ,"")  
 
        $shell = new-object -com shell.application
        $myFolder = $shell.Namespace($fileDir)
        $fileobj = $myFolder.Items().Item($fileName)
        $fontName = $myFolder.GetDetailsOf($fileobj,21)
        
        if ($fontName -eq "") { $fontName = $fileBaseName }  
 
        copy-item $filePath -destination $fontsFolderPath
 
        $fontFinalPath = Join-Path $fontsFolderPath $fileName
        if (-not($hashFontFileTypes.ContainsKey($fileExt))){Write-Host "File Extension Unsupported";$retVal = 0}  
        if ($retVal -eq 0) {
            Write-Host "Font `'$($filePath)`'`' installation failed on $env:computername" -ForegroundColor Red  
            Write-Host ""  
            1
        }
        
        else
        {
            Set-ItemProperty -path "$($fontRegistryPath)" -name "$($fontName)$($hashFontFileTypes.$fileExt)" -value "$($fileName)" -type STRING  
            Write-Host "Font `'$($filePath)`' $fontName $($hashFontFileTypes.$fileExt) installed successfully on $env:computername" -ForegroundColor Green  
        }
 
    }
    catch
    {
        Write-Host "An error occured installing `'$($filePath)`' on $env:computername" -ForegroundColor Red  
        Write-Host "$($error.ToString())" -ForegroundColor Red  
        $error.clear()
    }
    }
     }
     Remove-Item "$RemotePcStagingDir\$FontFile" -ErrorAction SilentlyContinue  
}
}
foreach($DisabledSvc in $DisabledSvcs){
    Write-Host "Setting service $DisabledSvc StartType to Disabled on $pc ..."  
    Set-Service -ComputerName $pc -Name $DisabledSvc -StartupType Disabled -ErrorAction SilentlyContinue
    $commandz="sc \\"+$pc +" Stop "+$DisabledSvc  
    & cmd.exe /c $commandz | Out-Null
}
}else{if($Winrmstatus){Write-Host "No Connection to Remote Pc $pc" -ForegroundColor Red}Else{Write-Host "No Connection to WinRM service on Remote Pc $pc" -ForegroundColor Red}}  
}
read-host	

In line 13, i'm trying to change it with the following folder picker dialog.
Function Get-Folder($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null  
    $Ordnername = New-Object System.Windows.Forms.FolderBrowserDialog
    $Ordnername.Description = "Ordner auswählen"  
    $Ordnername.rootfolder = "MyComputer"  
    if($Ordnername.ShowDialog() -eq "OK")  
    {
        $Ordner += $Ordnername.SelectedPath
    }
    return $Ordner
}
$folder = Get-Folder

Replacing the hardcoded folder with the
$folder
does not work. There is no error message.

Content-ID: 658826

Url: https://administrator.de/en/powershell-folder-picker-dialog-658826.html

Ausgedruckt am: 21.12.2024 um 14:12 Uhr

GarfieldBonn
GarfieldBonn 04.03.2021, aktualisiert am 16.05.2023 um 15:17:19 Uhr
Goto Top
Hello,

should it no be

$FontDir = Get-Folder
natalie.solero
natalie.solero 04.03.2021, aktualisiert am 16.05.2023 um 15:17:19 Uhr
Goto Top
Unfortunately i't doesn't work. The script closes directly without any error message.
GarfieldBonn
GarfieldBonn 04.03.2021, aktualisiert am 16.05.2023 um 15:17:19 Uhr
Goto Top
i tested it up to the foreach loop. It works fine.

How do you bind the function? Same script or via include-path?

The scipt works fine withput the function? Then it must be how how you bind it to the rest.