Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

IT-Wiki

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View my Codeberg Page

View my Photo Website

Hashtable

published: October 11, 2016 author: Tinu tags: Hashtable categories: PowerShell


Hashtable

Ein einfaches Beispiel einer Hashtable anhand einer Folderabfrage auf einem Mac:

Get-ChildItem -Path $home | %{
    @{$_.Name=(Split-Path -Parent $_.Fullname)}
}
Name                           Value                                  
----                           -----                                  
Applications                   /Users/xxx                            
Desktop                        /Users/xxx                            
Documents                      /Users/xxx                            
Downloads                      /Users/xxx                            
Movies                         /Users/xxx                            
Music                          /Users/xxx                            
Pictures                       /Users/xxx                            
Public                         /Users/xxx                            

Die Syntax ist @{Name=Value}

@{$_.Name=(Split-Path -Parent $_.Fullname)}

Hashtable mit Source- und Targetfolder

Eine Hashtable mit Source- und Targetfolder abfüllen, danach kann ein Backup mit einer Schleife abgearbeitet werden:

$targetroot = 'Z:'
$datahash   = [ordered]@{
    'S:\Scripting'    = "$($targetroot)\Backup\Scripting"
    'D:\Dokumente'    = "$($targetroot)\Backup\Dokumente"
    'W:\WebSites'     = "$($targetroot)\Backup\WebSites"
    'C:\Daten\Tools'  = "$($targetroot)\Backup\Tools"
    'F:\Fotografie'   = "$($targetroot)\Fotografie\Kataloge"
    'H:\HyperV'       = "$($targetroot)\Virtualisierung\HyperV"
}
foreach($item in $datahash.keys){
    if(Test-Path $targetroot){
        Write-Output "Source $($item), Target $($datahash[$item])"
    }
}

← Previous Post ◇ [ Top ] ◇ Copyright © 2024 by tinuwalther ◇ [ Blog ] ◇ Next Post →