Jump to content

Script to import passwords from Passwordmanager


Ulf

Recommended Posts

Hi just wanted to share my powershell script for importing passwords from Passwordmanager XP.

We have a lot of passwords so we wanted to create different password lists depending on the folder structure in Passwordmanager XP.

 

Make sure the first line is mapping the values to this line, you can change the Notes to Description if you prefer that.

Title;Username;Account;URL;Password;Modified;Created;Expire on;Notes;Modified by
 

But the only values we import is actually Title;UserName;Password;Description;URL;Notes  they don't need to be in any particular order.

 

Enjoy... :)

 

# Powershell script to import passwords from Passwordmanager XP
# Written By Ulf in 2017-11-27
# 
# I take no responsibility for what you do with this script. Use at your own risk! but it worked for me  ;-)



$FolderID = "xxxx"                                     # FolderID of the folder that we will use for our imported data
$PasswordlistTemplate = "xxxx"                         # PasswordList ID for a Normal password list we will copy all settings and permissions from
$APIKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"           # your APIKey  needs to be the global key. 
$Passwordlisttrigger = "\["                            # Trigger to know if this is a new Section in the list and this will generate a new passwordlist to put passwords in
$filetoimport = "C:\temp\Old Passwd to import.txt"     # Path to the file that you want to import
$Logtofile = "c:\temp\passwordstate-importlog.txt"     # Logfile you want to create and log to
$yourserverURL = "servername.local"                    # The server who is hosting the Passwordstate API


try {
$Imported = Import-Csv -Delimiter (";") -Path $filetoimport   # <-------------- Modify your Delimiter if this is different

    } catch { 
                "Error Can't find file $filetoimport" > $Logtofile
                Break
                 }
$PasswordListID = $null


"Starting Bulk import of $($Imported.count) objects" > $Logtofile



foreach($i in $Imported)
    {
    if($($i.Title) -Match $Passwordlisttrigger )     #This means it shuld be a new passwordlist
        {
       
        $PasswordlistName = $i.Title.trim('[]') 
        $PasswordlistName = $PasswordlistName.replace('\',' ') 
        
        "Search for Passwordlist $PasswordlistName" >> $Logtofile
        $Testpasswordlist = $null
       
       try {
        $Testpasswordlist = Invoke-Restmethod -Method Get -Uri "https://$yourserverURL/api/searchpasswordlists/?PasswordList=$PasswordlistName" -Header @{ "APIKey" = "$APIKey" }
          } Catch {
                    "Can't find $PasswordlistName in the database" >> $Logtofile
                  }
           $PasswordList = $Testpasswordlist | Where-Object -property PasswordList -eq $PasswordlistName  #if we found more than one passwordlist choose the right one
           if ( $PasswordList.PasswordList -eq $PasswordlistName )    #If there is a passwordlist already
                {        
                    
                    "Searched for Passwordlist $PasswordlistName and found Passwordlist $($PasswordList.PasswordList) with ID $($PasswordList.PasswordListID) " >> $Logtofile
                    $PasswordListID = $PasswordList.PasswordListID
                   
                } else {
        "Create Passwordlist $PasswordlistName" >> $Logtofile

     $PSData = 
         @{
	    PasswordList=$PasswordlistName
	    Description=$PasswordlistName
	    CopySettingsFromPasswordListID=$PasswordlistTemplate
	    CopyPermissionsFromPasswordListID=$PasswordlistTemplate
	    NestUnderFolderID=$FolderID
        APIKey=$APIKey
            }
      
      $jsonData = $PSData | ConvertTo-Json

      $PasswordstateUrl = "https://$yourserverURL/api/passwordlists"
      
                    try {
         $result = Invoke-Restmethod -Method Post -Uri $PasswordstateUrl -ContentType "application/json" -Body ([System.Text.Encoding]::UTF8.GetBytes($jsonData))   
            } catch {
                # Dig into the exception to get the Response details.
                # Note that value__ is not a typo.
                "StatusCode: $($_.Exception.Response.StatusCode.value__) " >> $Logtofile
                "StatusDescription: $($_.Exception.Response.StatusDescription) " >> $Logtofile
                $jsonData >> $Logtofile
                $result >> $Logtofile
                $_ >> $Logtofile
                
                    }
      
      
      $PasswordListID = $result.PasswordListID
      "Passwordlist $PasswordlistName Created Successfully with ID $PasswordListID" >> $Logtofile
                 
                  }

        }

   else {
      
    "$($i.Title) is a Password so create new password record" >> $Logtofile
   

    #JSON data for the object
    $PSData = 
    @{
        PasswordListID=$PasswordListID
        Title=$($i.Title)
        UserName=$($i.UserName)
        Password=$($i.Password)
        Description=$($i.Description)
        URL=$($i.URL)
        Notes=$($i.Notes)
        APIKey=$APIKey
    }
      $jsondata = $PSData | ConvertTo-Json
 
    $PasswordstateUrl = "https://$yourserverURL/api/passwords"
    try {
    $result = Invoke-Restmethod -Method Post -Uri $PasswordstateUrl -ContentType "application/json" -Body ([System.Text.Encoding]::UTF8.GetBytes($jsonData))
        } catch {
                # Dig into the exception to get the Response details.
                # Note that value__ is not a typo.
                "StatusCode: $($_.Exception.Response.StatusCode.value__) " >> $Logtofile
                "StatusDescription: $($_.Exception.Response.StatusDescription) " >> $Logtofile
                $jsonData >> $Logtofile
                $_ >> $Logtofile

                }
    }

}

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...