Skip to content

Commit

Permalink
Merge pull request #25 from PetriAsi/develop
Browse files Browse the repository at this point in the history
Use scoped variables instead of sessionstate
  • Loading branch information
PetriAsi authored Aug 4, 2021
2 parents 38954e4 + 6297f79 commit 54cf0a5
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion WilmaPSWorker/Private/Get-CredFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
Converts credential to tmpfile
#>
function Get-CredFile(){
[CmdletBinding()]
param(
[parameter(Mandatory=$true)]
[pscredential]
$cred
)

$pwdfile = [System.IO.Path]::GetTempFileName()
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($cred.Password)
$UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
Expand Down
1 change: 1 addition & 0 deletions WilmaPSWorker/Private/Get-SHA1StringHash.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Builds SHA1 string for wilma login
#>
Function Get-SHA1StringHash() {
[CmdletBinding()]
param(

[Parameter(Mandatory=$true)]
Expand Down
1 change: 1 addition & 0 deletions WilmaPSWorker/Private/Remove-TempFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Overwrites or wipes temp files before removing.
#>
function Remove-TempFile {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$tmpfile
Expand Down
4 changes: 2 additions & 2 deletions WilmaPSWorker/Public/Connect-WPSWSession.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ function Connect-WPSWSession (){
try {
Write-Verbose "Calling New-WPSWSession -site $site"
$WPSWSession = New-WPSWSession -site $site
Write-Debug $WPSWSession.Result.LoginResult
$PsCmdlet.SessionState.PSVariable.Set("_WPSWSession",$WPSWSession )
Write-Debug "Login result: $($WPSWSession.Result.LoginResult)"
Set-Variable -Name '_WPSWSession' -Value $WPSWSession -Scope Script -Force

}
Catch {
Expand Down
17 changes: 10 additions & 7 deletions WilmaPSWorker/Public/Get-WPSWCurrentSession.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ Get-WPSWCurrentSession
function Get-WPSWCurrentSession (){
[CmdletBinding()]
param()
try {
Write-Verbose "Get-WPSWCurrentSession"
$WPSWSession = $PsCmdlet.SessionState.PSVariable.Get("_WPSWSession")
$WPSWSession.Value
begin {
try {
Write-Verbose "Get-WPSWCurrentSession -Begin"
$WPSWSession = Get-Variable -Name '_WPSWSession' -Scope Script
}
Catch {
Throw "Cannot get Current WPSWSession. have you set up your site settings with New-WPSWSite and Connect-WPSWSession"
}
}
Catch {
Throw "Cannot get Current WPSWSession. have you set up your site settings with New-WPSWSite and Connect-WPSWSession"
process {
$WPSWSession.Value
}

}
13 changes: 11 additions & 2 deletions WilmaPSWorker/Public/Get-WPSWMessage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ function Get-WPSWMessage (){
$Folder
)
begin{
$WPSWSession = Get-WPSWCurrentSession
Write-Verbose "Get-WPSWMessage - Begin"
try {
$WPSWSession = Get-WPSWCurrentSession
} catch {
Throw "Get-WPSWMessage - Cannot get Current WPSWSession."
}


$urimap =@{
'Inbox' = '/messages/index_json'
'Sent' = '/messages/index_json/outbox'
Expand All @@ -39,6 +46,8 @@ function Get-WPSWMessage (){

}
process {
write-verbose "Get-WPSWMessage - Session : $WPSWSession"

try {

if ($Message_id){
Expand All @@ -58,7 +67,7 @@ function Get-WPSWMessage (){
}
catch{
$ErrorMessage = $_.Exception.Message
Write-Error "Could get messages. $ErrorMessage"
Write-Error "Could not get messages. $ErrorMessage"
}
}
}
1 change: 1 addition & 0 deletions WilmaPSWorker/Public/Get-WPSWSite.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Gets all Wilma site settings or specific site settings
#>
function Get-WPSWSite(){
[CmdletBinding()]
param(
[string]$site
)
Expand Down
7 changes: 6 additions & 1 deletion WilmaPSWorker/WilmaPSWorker.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ Tools to interact with Visma Wilma and Primus with primusquery
Collection of tools that make working with Wilma little bit easier and more secure.
#>

#Current session variable

New-Variable -Name '_WPSWSession' -Value @{} -Scope Local -Force

# Dot source public/private functions
$public = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Public/*.ps1') -Recurse -ErrorAction Stop)
$private = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Private/*.ps1') -Recurse -ErrorAction Stop)

foreach ($import in @($public + $private)) {
try {
Import-module $import.FullName
. $import.FullName
} catch {
throw "Unable to import-module [$($import.FullName)]"
}
}


0 comments on commit 54cf0a5

Please sign in to comment.