I have requirement where I want to write some metrics to the application insight for monitoring a service at a regular interval.
I though that I would write this PowerShell script and schedule it accordingly.
Write-Output "Script Start"
$PSScriptRoot = Get-Location
$AI = "$PSScriptRoot\Microsoft.ApplicationInsights.dll"
[Reflection.Assembly]::LoadFile("$AI")
$InstrumentationKey = ""
$TelClient = New-Object "Microsoft.ApplicationInsights.TelemetryClient"
$TelClient.InstrumentationKey = $InstrumentationKey
$TrackMetric = New-Object "Microsoft.ApplicationInsights.DataContracts.MetricTelemetry"
$TrackMetric.Name = "PowershellTest"
$TrackMetric.Value = Get-Random -Minimum:1 -Maximum:100
$TelClient.TrackMetric($TrackMetric)
$TelClient.Flush()
Write-Output "Script End $TrackMetric.Value"
This PowerShell Script works, but after I moving that script to Runbook it is no longer working.
So, here is the issue.
I am not able to load the ApplicationInsight DLL inside the Runbook.
Any idea how to do that?
Exception Details
Exception calling "LoadFile" with "1" argument(s): "The system cannot find the file specified. (Exception from HRESULT:
0x80070002)"
Thanks
Siraj
Try following path for the assembly
"C:\Modules\Global\Azure\Compute\Microsoft.ApplicationInsights.dll"
The issue is in loading the DLL file. The Runbook is not able to find the file in this line:
$AI = "$PSScriptRoot\Microsoft.ApplicationInsights.dll"
[Reflection.Assembly]::LoadFile("$AI")
When you run a Runbook via Azure Automation, you don't have access to the local path as you normally do on a local machine or on premise. In Azure Automation, modules are placed in "C:\Modules".
Instead, use below code snippet, after you have uploaded the dll file:
[System.Reflection.Assembly]::LoadFrom("C:\Modules\Azure\Microsoft.ApplicationInsights.dll")
Closest Related Reference: Referencing DLL
Related
i have currently an issue with a powershell script i have developed.
The following script runs on my system and on another developer machine but not on any other system... I have tested it on a virtual Win10 hyper v but it's always the same error:
Exception calling "ReadLookupTables" with "0" argument(s): "Object reference not set to an instance of an object.
The script code:
$pwaUrl = "https://xyz.co"
$userName = "xyz"
$password = "xyz"
Import-Module "$PSScriptRoot\ProjectHelper\Microsoft.ProjectServer.dll"
$projHelper = New-Object Microsoft.ProjectServer.MsProject($pwaUrl, $username, $password, $true)
$secSvc = $projHelper.GetSecuritySvc();
$secGroups = $secSvc.readGroupList();
$secGroups
On the developer machines Visual Studio Enterprise is installed... I have added all the SDK's and .Net Runtime environments on my virtual machine but it does not work.
Thanks for help
I have found a solution for my issue.
I've opened the dll with the Microsoft dll disassembler and checked on the Manifest the required dll for this dll...
Than i compared the dlls on my system with the other system and copied the missing files into the "$PSScriptRoot\ProjectHelper\Microsoft.ProjectServer.dll" folder
now it's working...
I'm deep into a rabbit hole of why a simple powershell script (below) works via the ISE, but fails when run via InstallShield's Powershell custom action type.
I'm posting the InstallShield-ready version of the script. Trace-Info can be replaced with Write-Host and Set-Property can be commented out.
try
{
Import-Module ActiveDirectory
$all_gmsa_accts = Get-ADServiceAccount -Filter '*'
$valid_gmsa_accts = #( )
ForEach ($acct in $all_gmsa_accts) {
Trace-Info -LogMessage "Checking if account $($acct.Name) can be used on this machine"
if(Test-ADServiceAccount -Identity $acct.Name -WarningAction silentlyContinue) {
$valid_gmsa_accts += $acct.Name
}
}
$prop_value = $valid_gmsa_accts -join ','
$prop_name = "MANAGEDSERVICEACCOUNTS"
Set-Property -Name $prop_name -Value $prop_value
}
catch {
Trace-Info -LogMessage "Exception:"
Trace-Info -LogMessage $_.Exception.Message
}
When I run on either PS command line or PS ISE, I get expected results:
PS D:\ps> .\get-mgmd-test.ps1
Checking if account gmsauser1 can be used on this machine
Checking if account gmsauser2 can be used on this machine
Checking if account cantuseme can be used on this machine
Checking if account tsrcsvcuser1 can be used on this machine
Checking if account tsrcsvcuser2 can be used on this machine
Checking if account tsrcsvcuser3 can be used on this machine
Checking if account tsrcsvcuser4 can be used on this machine
Checking if account tsrcsvcuser5 can be used on this machine
When run from InstallShield, I get the below error messages in the log output.
Action start 11:34:42: GetManagedServiceAccounts.
MSI (c) (64:E8) [11:34:42:266]: Invoking remote custom action. DLL: C:\Users\ADMINI~1\AppData\Local\Temp\MSI9B2D.tmp, Entrypoint: m2
InstallShield: Attempting to load through CLR 4 APIs...
InstallShield: Getting meta host...
InstallShield: Enumerating available runtimes...
InstallShield: Highest available runtime: v4.0.30319
InstallShield: Trying to use highest runtime...
InstallShield: Using highest version runtime...
InstallShield: Loading assembly ClrPsHelper from resource 4097
InstallShield: Calling method with parameters [(System.UInt32)165, (System.String)C:\Users\administrator\AppData\Local\Temp\d397fb4e-db0a-445a-9dc8-7ee4520e6436\getmanagedsvcaccts.ps1]
PowerShell wrapper: Exception:
PowerShell wrapper: Could not load file or assembly 'Microsoft.ActiveDirectory.Management' or one of its dependencies. Invalid pointer (Exception from HRESULT: 0x80004003 (E_POINTER))
Action ended 11:34:44: GetManagedServiceAccounts. Return value 1.
So, does anyone have insight into the specific mechanics of the InstallShield powershell CA type shim(s)?
Why would it be trying to load Microsoft.ActiveDirectory.Management when the ISE does not?
Is there some way I can get the PS command line to function in the same profile as msiexec?
I am trying to connect to an Azure Run As connection, as part of a Powershell script that does a backup of a database.
This script attempts to call Get-AutomationConnection
As seen in the screenshot, Get-Module does return that Azure / Azure.Storage and AzureRM shows.
What module should I import in addition for this to work?
If you want to connect to an Azure Run As connection from Windows PowerShell, you should use New-AzureRmAutomationConnection.
$ConnectionAssetName = "AzureRunAsConnection"
$ConnectionFieldValues = #{"ApplicationId" = $Application.ApplicationId; "TenantId" = $TenantID.TenantId; "CertificateThumbprint" = $Cert.Thumbprint; "SubscriptionId" = $SubscriptionId}
New-AzureRmAutomationConnection -ResourceGroupName $ResourceGroup -AutomationAccountName $AutomationAccountName -Name $ConnectionAssetName -ConnectionTypeName AzureServicePrincipal -ConnectionFieldValues $ConnectionFieldValues
You are able to use the script to create the connection asset because when you create your Automation account, it automatically includes several global modules by default along with the connection type AzurServicePrincipal to create the AzureRunAsConnection connection asset.
Get-AutomationConnection runs in Azure runbook internally.
Please refer to connection assets in Azure Automation.
If you want similar functionality to runbooks on-premise, you can install AzureAutomationAuthoringToolkit. It will give you very similar functionality. I have one script that logs in using the service principal, whether it is running on-premise or in an Azure runbook. It uses the resources provided by AAATK when running on-premise, that simulate a runbook.
I did try using the version of Get-AutomationConnection that comes with the "Microsoft Monitoring agent" (Hybrid worker), but I have since read that it is different to the one that comes with AzureAutomationAuthoringToolkit, detailed in the "Known Issues" in the GitHub readme. I couldn't get it to work, so I reverted to AAATK's version.
I have an Azure Cloud Service Worker Role which needs a separate Windows Service installed to redirect application tracing to a centralized server. I've placed the installation binaries for this Windows Service in a Storage Account's file storage as shown below. I then have my startup task call a batch file, which in turn executes a power-shell script to retrieve the file and install the service
When Azure deploys a new instance of the role, the script execution fails with the following error:
Cannot find path
'\\{name}.file.core.windows.net\utilities\slab1-1.zip' because it does
not exist
However, when I run the script after connecting through RDP, all is fine. Does anybody know why this might be happening? Here is the script below...
cmdkey /add:$storageAccountName.file.core.windows.net /user:$shareUser /pass:$shareAccessKey
net use * \\$storageAccountName.file.core.windows.net\utilities
mkdir slab
copy \\$storageAccountName.file.core.windows.net\utilities\$package .\slab\$package
I always have problem here and there by using a script to access the mounted azure file drive. I believe this is more or less related to the drive is mounted only for the current user and may not always work the same when called from a script.
I ended up pulling files from azure file the hard way without network drive.
$source= $stroageAccountName
$sourceKey = $shareAccessKey
$sharename = "utilities"
$package = "slab1-1.zip"
$dest = ".\slab\" + $package
#Define Azure file share root
$ctx=New-AzureStorageContext $source $sourceKey
$share = get-AzureStorageShare $sharename -Context $ctx
Get-AzureStorageFileContent -share $share -Destination $dest -Path $package -confirm:$false
Code example here will get you a good start:
https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-files/
It would be harder to manage if you have more complex folder structure, but objects there are CloudFileDirectory and CloudFile, property and methods there works seamlessly for me in powershell 4.0
*Azure Powershell module is required for 'Get-AzureStorageFileContent' cmdlet
I'm trying to write a PowerShell script to automate EventHub creation using Azure PowerShell. I am following the documentation outlined here and have installed the Azure PowerShell module (v 1.0.3).
I have added the Microsoft ServiceBus library (v3.0) using the following
$scriptPath = Split-Path -parent $PSCommandPath
$dllPath = "$scriptPath\..\..\packages\WindowsAzure.ServiceBus.3.1.2\lib\net45-full\Microsoft.ServiceBus.dll"
Add-Type -Path $dllPath
But as soon as I try and use the Get-AzureSBNamespace command, e.g.
$CurrentNamespace = Get-AzureSBNamespace -Name $Namespace
I get the following error
Get-AzureSBNamespace : Object reference not set to an instance of an object.
the same is true of New-AzureSBNamespace. I have also tried logging into Azure within the same session using Login-AzureRmAccount, but get the same object null reference exception.
Is this a bug, or am i missing something not outlined in the documentation?
This is because it uses the Service Management API and not the Resource Manager API. For me I ran:
Add-AzureAccount
And then Get-AzureSBNamespace and other SB cmdlets started working just fine.
If you can use PS 5.0 give this a try: msdn