Page 1 of 1 1
Topic Options
#199720 - 2010-09-01 08:48 PM Convert PowerShell-script to Kix
Jan_Egil_Ring Offline
Just in Town

Registered: 2010-09-01
Posts: 3
Loc: Oslo, Norway
Background

When renaming a few hundred Active Directory usernames there was a need to also update the name of the local profile folders on client computers to reflect the new username.
The profiles would work using the old username, however, the request was to rename them.

Since the clients consists of a mix of Windows XP, Windows Vista and Windows 7, we plan to use either Kix or VBScript to make the necessary changes.

A csv-file with a list of all affected users are available to the script, containing the old username, the new username and the users` SID.

The script

Since PowerShell is what I know the best when it comes to scripting, I`ve written a sample PowerShell-script to show what I want to accomplish.

I would like to get some guidance on how to convert this script to Kix.
 Code:
#Import CSV-file with the following headers: old,new,sid
$users = Import-Csv -Path "\\domain.local\netlogon\renamed-users.csv"

#Determine OS-version and set profilepath variable
$OS = Get-WmiObject Win32_OperatingSystem
switch -wildcard ($OS.Version)
    { 
        "5.1*" {$profilepath = "C:\Documents and Settings\"}
        "6.0*" {$profilepath = "C:\Users\"} 
        "6.1*" {$profilepath = "C:\Users\"}
    }

foreach ($user in $users) {

#Check if SID is listed in the ProfileList registry key
if ($user.sid) {

#Rename profilepath in registry
if (Test-Path ("HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\"+$user.sid)) {
Set-ItemProperty -Path ("HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\"+$user.sid) -Name "ProfileImagePath" -Value ($profilepath+$user.new)

#Check if profilepath exist
if (Test-Path ($profilepath+$user.old)) {

#Rename profile folder
Rename-Item -Path ($profilepath+$user.old) -NewName ($profilepath+$user.new)
}

}

}

}



Edited by Jan_Egil_Ring (2010-09-01 09:40 PM)

Top
#199723 - 2010-09-02 02:50 AM Re: Convert PowerShell-script to Kix [Re: Jan_Egil_Ring]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
Kix plays nice with INI files where obviously PS plays nice with CSVs. That's not to say we can't work around it, but we will need you to supply an example of your ini file, something like your header line and the first line of data.

The rest should be easy.

Curious why you want to use Kix? I believe you are the first person to ask to convert PS to Kix. Don't get me wrong... glad to see it. \:\)

Top
#199729 - 2010-09-02 12:15 PM Re: Convert PowerShell-script to Kix [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
There are two functions I use that make quick work of CSV files - FileIO and CSV. FileIO loads a file into an array of lines or writes an array of lines to a file. CSV translates between a CSV formatted string and an array. Here's some sample code (assumes you've included the two UDFs).
 Code:
$aRecords = FileIO('.\data.csv', 'R') ; load file data to array
$NRecs = UBound($aRecords) ; get number of records
For $Rec = 0 to $NRecs
  $aFields = CSV($aRecord[$Rec]) ; convert CSV string to array


  ; Examine the appropriate fields from $aFields and perform the
  ; necessary task(s)
  ; If you need to modify the record,
  ; just change the element in the $aFields array - the code below
  ; handles the update


  $TRec = CSV($aFields)          ; convert from array to CSV string
  If $TRec <> $aRecord[$Rec]     ; was it changed?
    $aRecord[$Rec] = $TRec       ; replace it!
    $Modified = 1                ; show it was modified
  EndIf
Next
If $Modified                     ; was the data changed?
  If FileIO('.\data.csv', 'W', $aRecords)
    'Updated! ?
  Else
    'Update failed - ' @SERROR ?
  EndIf
EndIf
If you aren't modifying the data, the first three lines of the script show how to load the data into an array, enumerate the array, and convert the CSV Data to an array of fields. From there, the logic is pretty basic - use ReadValue to read the registry, compare the data, and Move to rename the path.

You'll want to grab the latest copies of these UDFs from the Resources page on my web site - they've been updated recently.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#199738 - 2010-09-02 10:14 PM Re: Convert PowerShell-script to Kix [Re: Allen]
Jan_Egil_Ring Offline
Just in Town

Registered: 2010-09-01
Posts: 3
Loc: Oslo, Norway
 Originally Posted By: Allen
Kix plays nice with INI files where obviously PS plays nice with CSVs. That's not to say we can't work around it, but we will need you to supply an example of your ini file, something like your header line and the first line of data.

The rest should be easy.

Curious why you want to use Kix? I believe you are the first person to ask to convert PS to Kix. Don't get me wrong... glad to see it. \:\)


Thanks for the info.
The majority of the client computers where I`m going to use the script run Windows XP, and PowerShell are not deployed to all computers.
I then had to look for alternate solutions, and since Kix are used for logonscripts in the domain, it might be a good choice for concistency.

Top
#199739 - 2010-09-02 10:16 PM Re: Convert PowerShell-script to Kix [Re: Glenn Barnas]
Jan_Egil_Ring Offline
Just in Town

Registered: 2010-09-01
Posts: 3
Loc: Oslo, Norway
The functions looks really interesting, I`ll give them a try. Thanks!
Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 977 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.052 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org