This week we had to make a PowerShell script that call a .NET assembly class and trigger an action on a worker role. We wrote the logic in C# because was more simple for us, even if we could write the logic in PowerShell.
The .NET assembly had to read some configurations from configuration file. When we wrote the C# code, it was tested using Unit Tests and it was very simple in that case – we used app.config for this.
But when you want to the call of the logic from PowerShell thing are changing a little.
How to attach VS to PowerShell
First thing that you should do in this moment is to attached to PowerShell process before running the script. You can do this from menu: Debug\Attach To Process. You need to select from the processes list the ‘powershell’ and attach to it.
Once this step is done, you will be able to find put any breaks points in the code or catch from Visual Studio all the exception that are thrown by you assembly. Don’t forget to run the script one more time for this.
Specify the configuration file of assembly
When you load an assembly from PowerShell, the configuration file is not loaded automatically. To load a configuration file for a given assembly you need to add in your script the following command
Conclusion
Don’t forget that running C# code from PowerShell is not like running console applications from a bat. Because of this you should be aware of these things.
The .NET assembly had to read some configurations from configuration file. When we wrote the C# code, it was tested using Unit Tests and it was very simple in that case – we used app.config for this.
But when you want to the call of the logic from PowerShell thing are changing a little.
[Reflection.Assembly]::LoadFile(' [assemblyFilePath].dll')
Even if we had the configuration file (app.config) in the same folder as our assembly, the PowerShell script would throw an expectation that the configuration was not found.How to attach VS to PowerShell
First thing that you should do in this moment is to attached to PowerShell process before running the script. You can do this from menu: Debug\Attach To Process. You need to select from the processes list the ‘powershell’ and attach to it.
Once this step is done, you will be able to find put any breaks points in the code or catch from Visual Studio all the exception that are thrown by you assembly. Don’t forget to run the script one more time for this.
Specify the configuration file of assembly
When you load an assembly from PowerShell, the configuration file is not loaded automatically. To load a configuration file for a given assembly you need to add in your script the following command
[System.AppDomain]::CurrentDomain.SetData(
"APP_CONFIG_FILE",
‘[configurationFilePath].config’)
Don’t forget that this call needs to be before assembly loading:[Reflection.Assembly]::LoadFile(' [assemblyFilePath].dll')
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", ‘[configurationFilePath].config’)
Conclusion
Don’t forget that running C# code from PowerShell is not like running console applications from a bat. Because of this you should be aware of these things.
Comments
Post a Comment