• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 96
  • Last Modified:

How to Find Files for Microsoft Azure Powershell

Hello Experts,

Can someone please let me know how to obtain the System.IO.File, and System.Convert

I am trying to  add the following Azure Powershell command:

$bytes = [System.IO.File]::ReadAllBytes($keysPath)

$base64 = [System.Convert]::ToBase64String($bytes)

Open in new window


However, I don't know where to find the System.IO.File and System.Convert

Thanks

Carlton
0
Member_2_7966113
Asked:
Member_2_7966113
  • 7
  • 2
  • 2
2 Solutions
 
Lawrence TsePrinciple ConsultantCommented:
It's calling .NET framework from PowerShell.  They are available as long as your system has .NET framework installed, which is default available for most up-to-date Windows OS.  You should be able to run this command directly from PowerShell.
0
 
Member_2_7966113Author Commented:
Hi Lawrence,

I get the following errors when I enter the commands directly in powershell

kop
0
 
Member_2_7966113Author Commented:
Lawrence

The other command

kop
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Member_2_7966113Author Commented:
Lawrence,

The full script is as follows:

$subscriptionId = 'xxxx.xxxxxx.xxxxx.xxxx'; #Pre Production        
        $resourceGroup = 'mykevaultrg'
        $location = 'WestUS'
        $vaultName = 'mykevaultname'
        $keyname = 'mykevaultname'
        $password = 'Patters0n'
        continue
        }
}


Select-AzureRmSubscription -SubscriptionId $subscriptionId
Set-AzureRmContext -SubscriptionId $subscriptionId

if(Get-AzureRmResourceGroup -Name $resourceGroup){
    Write-Output "Deleteing Resource Group $resourceGroup"
    Remove-AzureRmResourceGroup -Name $resourceGroup -Force
}
else
{
    Write-Output 'No Resource Group found'
}

New-AzureRmResourceGroup -Name $resourceGroup -Location $location
New-AzureRmKeyVault -VaultName $vaultName -resourceGroup $resourceGroup -Location $location -EnabledForDeployment -EnabledForTemplateDeployment

Import-Module "$THIS_SCRIPTS_DIRECTORY\ServiceFabricRPHelpers.psm1"

$keypath = "c:\azurekeys\$keyname.pfx"

$passwordEnc = ConvertTo-SecureString -String $password -AsPlainText -Force

$bytes = [System.IO.File]::ReadAllBytes($keysPath)
$base64 = [System.Convert]::ToBase64String($bytes)

$jsonBlob = @{ 
    data = $base64
    dataType = 'pfx'
    password = $password
        } | ConvertTo-Json 
            $contentBytes = [System.Text.Encoding]::UTF8.GetBytes($jsonBlob) 
            $content = [System.Convert]::Tobase64String($contentBytes) 
            $secretValue = ConvertTo-SecureString -String $content -AsPlainText –Force
            Set-AzureKeyVaultSecret -VaultName $vaultName -Name $keyname -SecretValue $secretValue


$cert = new-object System.Security.Cryptography.X509Certificates.X509Certificate2 $keysPath, $passwordEnc
$cert.Thumbprint


#Create Self Signed Cert
Invoke-AddCertToKeyVault -SubscriptionId $subscriptionId -resourceGroup $resourceGroup -Location $location -VaultName $vaultName -CertificateName $keyname -Password $pwd -CreateSelfSignedCertificate -DnsName 'cds-pp-02-careersvc.westeurope.cloudapp.azure.com' -OutputPath 'C:\certs\'
#Use existing cert
#Invoke-AddCertToKeyVault -SubscriptionId $subscriptionId -resourceGroup $resourceGroup -Location $location -VaultName $vaultName -CertificateName cds-prd-01-careersvc -Password $pwd -UseExistingCertificate -ExistingPfxFilePath $keypath

[System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes("$keylocation.pfx")) | Out-File "$keylocation.txt"

Get-PfxCertificate -FilePath $keypath\NameOfCert.pfx

Open in new window

0
 
Lawrence TsePrinciple ConsultantCommented:
I think the source of the problem is on line 29 it was asking for a variable $keyname but it was not defined before.  That's why in your execution you can see it was saying "c:\azurekey\.pfx" not found.

You can in line one define:

$keyname="PFX_file_name_without_extension"

Open in new window


Then it will make the execution becomes "c:\azurekey\PFX_file_name_without_extension.pfx" and everything should work.

Cheers.
0
 
Member_2_7966113Author Commented:
Hi Lawrence,

I had to step away from my desk.

Just looking at your suggestion now.

Cheers
0
 
David Johnson, CD, MVPOwnerCommented:
$keypath = "c:\azurekeys\$keyname.pfx"
$bytes = [System.IO.File]::ReadAllBytes($keysPath)

either use  $keypath or $keyspath
0
 
Member_2_7966113Author Commented:
Hi David,

Your suggestion worked, however the script fails at the following:

[System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes("$keylocation.pfx")) | Out-File "$keylocation.txt"

Open in new window


I get the following error message, see image:

kop
Can you assist with this error?

The full script is as follows:

$subscriptionId = 'xxxx.xxxxx.xxxxxx.xxxxxx'; #Pre Production        
        $resourceGroup = 'mykevaultrg'
        $location = 'WestUS'
        $vaultName = 'mykevault'
        $keyname = 'mykevaultname'
        $password = 'Patters0n'
        

Select-AzureRmSubscription -SubscriptionId $subscriptionId
Set-AzureRmContext -SubscriptionId $subscriptionId

if(Get-AzureRmResourceGroup -Name $resourceGroup){
    Write-Output "Deleteing Resource Group $resourceGroup"
    Remove-AzureRmResourceGroup -Name $resourceGroup -Force
}
else
{
    Write-Output 'No Resource Group found'
}

New-AzureRmResourceGroup -Name $resourceGroup -Location $location
New-AzureRmKeyVault -VaultName $vaultName -resourceGroup $resourceGroup -Location $location -EnabledForDeployment -EnabledForTemplateDeployment

Import-Module "$THIS_SCRIPTS_DIRECTORY\ServiceFabricRPHelpers.psm1"

$keypath = "c:\azurekeys\$keyname.pfx"

$passwordEnc = ConvertTo-SecureString -String $password -AsPlainText -Force

$bytes = [System.IO.File]::ReadAllBytes($keypath)
$base64 = [System.Convert]::ToBase64String($bytes)

$jsonBlob = @{ 
    data = $base64
    dataType = 'pfx'
    password = $password
        } | ConvertTo-Json 
            $contentBytes = [System.Text.Encoding]::UTF8.GetBytes($jsonBlob) 
            $content = [System.Convert]::Tobase64String($contentBytes) 
            $secretValue = ConvertTo-SecureString -String $content -AsPlainText –Force
            Set-AzureKeyVaultSecret -VaultName $vaultName -Name $keyname -SecretValue $secretValue


$cert = new-object System.Security.Cryptography.X509Certificates.X509Certificate2 $keysPath, $passwordEnc
$cert.Thumbprint


#Create Self Signed Cert
Invoke-AddCertToKeyVault -SubscriptionId $subscriptionId -resourceGroup $resourceGroup -Location $location -VaultName $vaultName -CertificateName $keyname -Password $pwd -CreateSelfSignedCertificate -DnsName 'cds-pp-02-careersvc.westeurope.cloudapp.azure.com' -OutputPath 'C:\certs\'
#Use existing cert
#Invoke-AddCertToKeyVault -SubscriptionId $subscriptionId -resourceGroup $resourceGroup -Location $location -VaultName $vaultName -CertificateName cds-prd-01-careersvc -Password $pwd -UseExistingCertificate -ExistingPfxFilePath $keypath

[System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes("$keylocation.pfx")) | Out-File "$keylocation.txt"

Get-PfxCertificate -FilePath $keypath\NameOfCert.pfx

Open in new window

0
 
Member_2_7966113Author Commented:
Hi Experts

Anymore thoughts on this?
0
 
David Johnson, CD, MVPOwnerCommented:
$keylocation and $keypath are not defined in the given script
0
 
Member_2_7966113Author Commented:
Thanks guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

  • 7
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now