Script powershell

Welcome Forums Pester Script powershell

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
2 months, 2 weeks ago.

  • Author
    Posts
  • #99160

    Participant
    Points: 0
    Rank: Member

    Bonjour, jai fait un script powershell et j'aimerais avoir une aide, car le script ne fonctionne pas normalement je vous joins se que je dois faire, et le script que j'ai écrit, mais il ne renomme par mon répertoire.

    The Powershell script should do the following:
    – Test if directory "\\ ecarbssf0007 \% username% \ Home \ Documents" exists. If YES, Exit.
    – Test if directory "\\ ecarbssf0007 \% username% \ Home \ My Documents" exists. If YES, Rename directory in "Documents"

    This powershell script will be placed upstream of the redirection process of user directories.

    and here is my script

    $Path = "C:\My Documents"
    $OldText = "My Documents"
    $NewText = "Documents"
    $logFile = 'c:\temp\test.txt'

    $a = Get-Date
    "Lancement du Script de renommage : " + $a >> $logfile
    Get-ChildItem $Path -Recurse | %{$_.FullName} |
    Sort-Object -Property Length -Descending |
    % {cd\
    Write-Host $_
    $Item = Get-Item $_
    $PathRoot = $Item.FullName | Split-Path
    $OldName = $Item.FullName | Split-Path -Leaf
    $NewName = $OldName -replace $OldText, $NewText
    $NewPath = $PathRoot | Join-Path -ChildPath $NewName

    if ($OldName.Contains($OldText)) {
    {
    $a = Get-Date
    $erreurInfo = "Impossible de modifier " + $item.Fullname + " "+ $a
    $erreurInfo >> $logFile
    }

    }

    }

    $a = Get-Date
    "FIN : " + $a >> $logfile

  • #99162

    Participant
    Points: 0
    Rank: Member

    Hello, I made a powershell script and I would like help, because the script does not work normally I join you what I have to do, and the script that I wrote, but it does not rename by my directory.

    The Powershell script should do the following:
    – Test if directory "\\ ecarbssf0007 \% username% \ Home \ Documents" exists. If YES, Exit.
    – Test if directory "\\ ecarbssf0007 \% username% \ Home \ My Documents" exists. If YES, Rename directory in "Documents"

    This powershell script will be placed upstream of the redirection process of user directories.

    and here is my script

    $Path = "C:\My Documents"
    $OldText = "My Documents"
    $NewText = "Documents"
    $logFile = 'c:\temp\test.txt'

    $a = Get-Date
    "Lancement du Script de renommage : " + $a >> $logfile
    Get-ChildItem $Path -Recurse | %{$_.FullName} |
    Sort-Object -Property Length -Descending |
    % {cd\
    Write-Host $_
    $Item = Get-Item $_
    $PathRoot = $Item.FullName | Split-Path
    $OldName = $Item.FullName | Split-Path -Leaf
    $NewName = $OldName -replace $OldText, $NewText
    $NewPath = $PathRoot | Join-Path -ChildPath $NewName

    if ($OldName.Contains($OldText)) {
    {
    $a = Get-Date
    $erreurInfo = "Impossible de modifier " + $item.Fullname + " "+ $a
    $erreurInfo >> $logFile
    }

    }

    }

    $a = Get-Date
    "FIN : " + $a >> $logfile

    • #110020

      Participant
      Points: 527
      Helping Hand
      Rank: Major Contributor

      There are many issues in this script. What you would need is pure basics. I would suggest you to start from here and a highly recommended book "Learn PowerShell Toolmaking in a Month of Lunches" by Don Jones .

You must be logged in to reply to this topic.