Reputation: 1375
I'm trying to teach myself Azure Powershell scripting and have an ultimate goal of setting up a script that reads in an Excel Spreadsheet with specifications for Azure VMs to create (things like, type of VM, tags, timezone, which AD group to add it to, etc). If anybody has any tutorial references for this, that would be very helpful.
Currently, I'm falling on my face on what should be something relatively simple. Exporting functions. I have opened Powershell ISE and tried to run the following code (taken from one of the examples I found on MSDN):
Function New-Test
{
Write-Output 'I am New-Test function'
}
Export-ModuleMember -Function New-Test
function Validate-Test
{
Write-Output 'I am Validate-Test function'
}
function Start-Test
{
Write-Output 'I am Start-Test function'
}
Set-Alias stt Start-Test
Export-ModuleMember -Function Start-Test -Alias stt
But I get an error saying: "Export-ModuleMember: Object Reference not set to an instance of an object"
I have tried saving this code out to a ps1 file named test and then navigating to the directory it's in and running "./test.ps1" but the same error comes up.
Any idea on what I'm doing wrong here? There is surely something fundamental that I am missing here.
Upvotes: 2
Views: 2100
Reputation: 96
In my case the solution was to reference the psm1 instead of the ps1 ind the psd1 file
# Script module or binary module file associated with this manifest.
RootModule = 'pf-rootscript.psm1'
instead of
# Script module or binary module file associated with this manifest.
RootModule = 'pf-rootscript.ps1'
Upvotes: 1
Reputation: 361
I ran into this error, too. For myself, I found out that the "Export-ModuleMember" can't have any line breaks between it and the end of the last function bracket.
Examples:
Import-Module Object Reference Error:
function Test-Import{
Write-Host "import function success"
}
Export-ModuleMember -Function Test-Import
Import-Module, no errors:
function Test-Import{
Write-Host "import function success"
}
Export-ModuleMember -Function Test-Import
Upvotes: 1
Reputation: 9684
When you plan to write a lot of code in PowerShell scripts, then setting up your module and controlling what you export (or not) using Export-ModuleMember is the right thing to do. So if you're planning on building your own module to consume in further PowerShell scripts, then you're on the right track.
You haven't mentioned anything about the module definition or how/where exactly you are consuming these functions, so I think you are probably missing the part where you define your module first.
You can follow a step by step guide for doing that here: PowerShell: Building a Module, one microstep at a time
Upvotes: 0