Tag Archives: classes

Simple Powershell MRU list

When using using my secret server powershell functions I got tired of constantly searching for secret ID’s. I had to do a new search just because I could not remember the ID’s. So I added some kind of MRU to my get-secretID function. This code block creates to classes mruitem and mrulist. The mrulist has three functions. Updatelist: Check if the ID already is in the list , if so update last used time. If it is not in the list , add it. Also remove oldest item if list is longer then max size. Savelist: saves the list to mru file. Loadlist : Load old list from file. Script also uses a global variable name ssmru. The global variable is declared in the powershell profile as $ssmru = “<filepath to mru list>”. I use a file because I want this MRU to stay persistent during a reboot.

# MRU list and item class 
class mruitem{
[string]$ssid
[string]$name
[datetime]$lastuseddate

mruitem([string]$ssid,[string]$name,[datetime]$lastuseddate)
{
$this.ssid=$ssid
$this.lastuseddate=$lastuseddate
$this.name=$name
}
}

class mrulist: System.Collections.ArrayList {

[int]$MaxSize = 15

updatelist([mruitem]$item){
$pos=$null
if($this.count -gt 0){
if($this.ssid.contains($item.ssid)){
$pos=$this.ssid.indexof($item.ssid)
}
else {$pos=$null}

if($pos){
$this[$pos].lastuseddate=$item.lastuseddate
$this[$pos].name=$item.name
}else{ #Add new
$this.add($item)
}
}else {
$this.add($item)
}
if($this.count -gt $this.MaxSize){
#delete oldest
$new = New-Object System.Collections.ArrayList
$new=($this| Sort-Object -Property lastuseddate )
$ssid=$new[0].ssid
$ssid
$pos=$this.ssid.indexof($ssid)
$this.RemoveAt($pos)
$new=$null
}
}

loadlist(){
if(Test-Path $global:ssmru){
$data=(Get-Content -Path $global:ssmru -Encoding Unicode)|ConvertFrom-Json
foreach($mru in $data){
$this.updatelist([mruitem]::new($mru.ssid,$mru.name,$mru.lastuseddate))
}
}
}

savelist(){
$data=$this|ConvertTo-Json
$data|out-file -FilePath $global:ssmru -Encoding unicode
}
}

$mrulist=New-Object -typename mrulist
$mrulist.loadlist()
if($mru){
$mrulist | Sort-Object -Property lastuseddate
return
}

For those interested , here is the updated version of get-secretID

function Get-SecretID
{
param(
[parameter(ValueFromPipeline=$True)]
[int] $secretID,
[pscredential]$sscred,
[switch]$Cleartext,
[switch]$mru
)
if(!($secretID)){$mru=$true}

# MRU list and item class
class mruitem{
[string]$ssid
[string]$name
[datetime]$lastuseddate

mruitem([string]$ssid,[string]$name,[datetime]$lastuseddate)
{
$this.ssid=$ssid
$this.lastuseddate=$lastuseddate
$this.name=$name
}
}

class mrulist: System.Collections.ArrayList {

[int]$MaxSize = 15

updatelist([mruitem]$item){
$pos=$null
if($this.count -gt 0){
if($this.ssid.contains($item.ssid)){
$pos=$this.ssid.indexof($item.ssid)
}
else {$pos=$null}

if($pos){
$this[$pos].lastuseddate=$item.lastuseddate
$this[$pos].name=$item.name
}else{ #Add new
$this.add($item)
}
}else {
$this.add($item)
}
if($this.count -gt $this.MaxSize){
#delete oldest
$new = New-Object System.Collections.ArrayList
$new=($this| Sort-Object -Property lastuseddate )
$ssid=$new[0].ssid
$ssid
$pos=$this.ssid.indexof($ssid)
$this.RemoveAt($pos)
$new=$null
}
}

loadlist(){
if(Test-Path $global:ssmru){
$data=(Get-Content -Path $global:ssmru -Encoding Unicode)|ConvertFrom-Json
foreach($mru in $data){
$this.updatelist([mruitem]::new($mru.ssid,$mru.name,$mru.lastuseddate))
}
}
}

savelist(){
$data=$this|ConvertTo-Json
$data|out-file -FilePath $global:ssmru -Encoding unicode
}
}

$mrulist=New-Object -typename mrulist
$mrulist.loadlist()
if($mru){
$mrulist | Sort-Object -Property lastuseddate
return
}

$where = 'https://<Server FQDN>/secretserver/winauthwebservices/sswinauthwebservice.asmx'

if($sscred -ne $null){
try{
$ws = New-WebServiceProxy -uri $where -Credential $sscred
}
catch{
Write-host "Error: Error connecting to secret server."
return $null
}
}else{

try{
$ws = New-WebServiceProxy -uri $where -UseDefaultCredential -ErrorAction SilentlyContinue
if($ws -eq $null){
if (!(Test-Path Variable:\ssadmin)){
throw {
Write-host "No secretserver admin specified or variable 'ssadmin' defined.`nThis is to be used by 'get-storedcredential'"
}
}
$adminacc=Get-StoredCredential -UserName $ssadmin
$ws = New-WebServiceProxy -uri $where -Credential $adminacc -ErrorAction SilentlyContinue
if($ws -eq $null){throw{Write-host "Unable to connect to SecretServer"}}
}
}
catch{
Write-host "Error connecting to SecretServer"
return $null
}
}

$wsResult = $ws.GetSecret($secretId, $false, $null)
if($wsresult.errors -ne $null){
$Cred=New-Object PSObject
$Cred | add-member -NotePropertyName "Username" -NotePropertyValue $wsresult.errors
$Cred | Add-Member -NotePropertyName "Password" -NotePropertyValue $wsresult.errors

return $Cred
} else {

$u=$wsResult.Secret.Items[1].value.ToString()
$ep = ConvertTo-SecureString $wsResult.Secret.Items[2].value.ToString() -AsPlainText -Force
[pscredential]$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $u,$ep
if($Cleartext){
[psobject]$Cred=New-Object PSObject
$Cred | add-member -NotePropertyName "Username" -NotePropertyValue $u
$Cred | Add-Member -NotePropertyName "Password" -NotePropertyValue $wsResult.Secret.Items[2].value.ToString()
$Cred | Add-Member -NotePropertyName "Domain" -NotePropertyValue $wsResult.Secret.Items[0].value.ToString()
}
$mrulist.updatelist([mruitem]::new($secretID,$u,(get-date)))
$mrulist.savelist()
return $Cred
}
}