Cartwheel.IO.psm1



function Set-Directory{

    [OutputType([System.Collections.Hashtable])]
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$DirectoryPath,
        [Parameter(Mandatory)]   
        [ValidateSet("Present", "Absent")]
        [string]$Ensure 
        )

    $testTargetResourceResult = Test-Path -PathType Container -path $DirectoryPath  -ErrorVariable ItemErr -ErrorAction SilentlyContinue ;
    $test = Test-Target $Ensure  $testTargetResourceResult $ItemErr $DirectoryPath

    if ($test.Command -eq "Add") {
        $output = New-Item -ItemType Directory -Force -Path $DirectoryPath
        Write-Host "Item ($DirectoryPath) in wrong state... added."
    }    
    if ($test.Command -eq "Remove") {
        $output = Remove-Item -Recurse -Force $DirectoryPath
        Write-Host  "Item ($DirectoryPath) in wrong state... removed."
    }
    if ($test.Command -eq "Skip") {
        Write-Host  "Item ($DirectoryPath) in correct state."
    }
    if ($test.Command -eq "Error") {
        Write-Host  "Item ($DirectoryPath) had errors."
    }
    return $test
}



function Set-AzureShareItem{

[OutputType([System.Collections.Hashtable])]
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ItemSourcePath,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ItemDestinationPath,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$StorageAccountName,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$StorageAccountKey,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ShareName,
    
        [Parameter(Mandatory)]   
        [ValidateSet("Present", "Absent")]
        [string]$Ensure = "Present"
        )




# Dependency
if (-Not(Get-Module -ListAvailable AzureRM.Storage)) {Install-Module -Name AzureRM.Storage -Force -SkipPublisherCheck}

$DirectoryPath=split-path -Path $ItemDestinationPath
Set-Directory -DirectoryPath $DirectoryPath  -ensure "Present" >$null


$testTargetResourceResult = Get-Item -path $ItemDestinationPath  -ErrorVariable ItemErr -ErrorAction SilentlyContinue ;
$exists = $false
if ($testTargetResourceResult){$exists = $true}
$test = Test-Target $Ensure $exists "" $ItemDestinationPath


    if ($test.Command -eq "Add") {
                $ctx = New-AzureStorageContext ` -StorageAccountName $StorageAccountName  `
                -StorageAccountKey $StorageAccountKey

                Get-AzureStorageFileContent `
                -Context $ctx `
                -ShareName $ShareName `
                -Path $ItemSourcePath `
                -Destination $ItemDestinationPath
                Write-Verbose  "Item ($ItemDestinationPath) in wrong state... added."
    }
    if ($test.Command -eq "Remove") {
        Remove-Item -Recurse -Force $ItemDestinationPath
        Write-Verbose  "Item ($ItemDestinationPath) in wrong state... removed."
    }
    if ($test.Command -eq "Skip") {
        Write-Verbose  "Item ($ItemDestinationPath) in correct state."
    }
    if ($test.Command -eq "Error") {
        Write-Verbose  "Item ($ItemDestinationPath) had errors."
    }

return $test

}


function Format-XML ([xml] $xml, $indent=2){
    $StringWriter = New-Object System.IO.StringWriter
    $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter
    $xmlWriter.Formatting = �indented�
    $xmlWriter.Indentation = $Indent
    $xml.WriteContentTo($XmlWriter)
    $XmlWriter.Flush()
    $StringWriter.Flush()
    return $StringWriter.ToString()
}

function Clean-XML{
[CmdletBinding()]
    param(            
        [Parameter(Mandatory = $true)]
        [string]$line 
    )
$lookupTable = @{
    '"' = '"'
    '''' = '''
    '<' = '&lt;'
    '>' = '&gt;'
    '&' = '&amp;'

}
     $lookupTable.GetEnumerator() | ForEach-Object {
        if ($line -match $_.Key)
        {
            $line = $line -replace $_.Key, $_.Value
        }
    }
  return $line

}

function Get-HashProperty{

Param(
   [OutputType([PSObject])]
   [Parameter(Mandatory = $true)]
   [System.Collections.Hashtable]$HashTable,
   [Parameter(Mandatory = $true)]
   [String]$KeyName,
   [Parameter(Mandatory = $true)]
   [String]$PropertyName
   )





$HashTable.GetEnumerator() | foreach {
    $name = $_.Name
    if($name -eq $KeyName)
    { 
        foreach($key in $_.Value.keys){
            if($key -eq $PropertyName)
            {
                 return  $_.Value[$key]  #fixes unrolling into array on return
            }
        } 
    }
}

return $null

}