Beginner Question :-)

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
treierp
Posts: 7
Joined: Tue Aug 28, 2018 11:42 pm

Beginner Question :-)

Post by treierp » Wed Aug 29, 2018 11:51 pm

Good Morning
I'm new to PS and got some start problems...

Look at my module

Code: Select all

Using module ..\..\Helper\OutputFormatter.psm1

Function CollectFirewall
{
	$prefix = "FireWall"
	
	$domainprofile = netsh advfirewall show domainprofile state
	$domainprofile = $domainprofile.GetValue(3).Remove(0, 5).Trim()
	AddHardWare -code ($prefix + "DomainProfile") -value $domainprofile
	
	$privateprofile = netsh advfirewall show privateprofile state
	$privateprofile = $privateprofile.GetValue(3).Remove(0, 5).Trim()
	AddHardWare -code ($prefix + "PrivateProfile") -value $privateprofile
	
	$publicprofile = netsh advfirewall show publicprofile state
	$publicprofile = $publicprofile.GetValue(3).Remove(0, 5).Trim()
	AddHardWare -code ($prefix + "PublicProfile") -value $publicprofile
}

Export-ModuleMember CollectFirewall
And that's what's been created in the run.ps1

Code: Select all

#region Source: Hardware\SpecificCollector\FirewallCollector.psm1
function 
{
	Function CollectFirewall
	{
		$prefix = "FireWall"
		
		$domainprofile = netsh advfirewall show domainprofile state
		$domainprofile = $domainprofile.GetValue(3).Remove(0, 5).Trim()
		AddHardWare -code ($prefix + "DomainProfile") -value $domainprofile
		
		$privateprofile = netsh advfirewall show privateprofile state
		$privateprofile = $privateprofile.GetValue(3).Remove(0, 5).Trim()
		AddHardWare -code ($prefix + "PrivateProfile") -value $privateprofile
		
		$publicprofile = netsh advfirewall show publicprofile state
		$publicprofile = $publicprofile.GetValue(3).Remove(0, 5).Trim()
		AddHardWare -code ($prefix + "PublicProfile") -value $publicprofile
	}
	
	Export-ModuleMember CollectFirewall
}
#endregion Source: FirewallCollector.psm1
As you can see, two times the Function Key Word?
What did I wrong ?

Any help is welcome :-)

Peter

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Beginner Question :-)

Post by jvierra » Thu Aug 30, 2018 12:21 am

How are you creating the second script?

There is not enough information to understand what your issue is.

treierp
Posts: 7
Joined: Tue Aug 28, 2018 11:42 pm

Re: Beginner Question :-)

Post by treierp » Thu Aug 30, 2018 12:31 am

Here's my complete project...

Rename it *.txt -> *.rar
Attachments
SISA.Inventar.txt
(127.16 KiB) Downloaded 29 times

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Beginner Question :-)

Post by jvierra » Thu Aug 30, 2018 12:38 am

By the way:

To get all network fw profiles: Get-NetConnectionProfile
To get the current active profile: (Get-NetConnectionProfile).NetworkCategory

Each active adapter can be set to a different profile.

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Beginner Question :-)

Post by jvierra » Thu Aug 30, 2018 12:41 am

treierp wrote:
Thu Aug 30, 2018 12:31 am
Here's my complete project...

Rename it *.txt -> *.rar
Sorry but I don't do RAR.

You can upload the folder to the project as a ZIP file. RAR files won't upload and the web server and Windows will trash the TXT file.

treierp
Posts: 7
Joined: Tue Aug 28, 2018 11:42 pm

Re: Beginner Question :-)

Post by treierp » Thu Aug 30, 2018 12:54 am

Here's the Zip File.
Thank's for the hint with the Firewall Profile!
Attachments
SISA.Inventar.zip
(138.32 KiB) Downloaded 34 times

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Beginner Question :-)

Post by jvierra » Thu Aug 30, 2018 1:03 am

I don't see the issue. What is it that is not working?

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Beginner Question :-)

Post by jvierra » Thu Aug 30, 2018 1:06 am

It looks to me like you are using the wrong kind of project for a module. You need to create a "module project".

treierp
Posts: 7
Joined: Tue Aug 28, 2018 11:42 pm

Re: Beginner Question :-)

Post by treierp » Thu Aug 30, 2018 1:09 am

As soon as I start the project (F5) this errors are shown in the Output Window

Code: Select all

>> Running (SISA.Inventar.psproj) Project Script...
>> Building (SISA.Inventar) Project...
>> Merging 'Startup.pss' ...
>> Merging 'FirewallCollector.psm1' ...
>> Merging 'MachineCollector.psm1' ...
>> Merging 'OSCollector.psm1' ...
>> Merging 'Globals.ps1' ...
>> Writing 'SISA.Inventar.Run.ps1' ...
>> Platform: V5 64Bit (STA) (Elevated)
SISA.Inventar.Run.ps1: ERROR: At Line: -2 char: 9
ERROR: + function
ERROR: + ~
ERROR: Missing name after function keyword.
ERROR:
SISA.Inventar.Run.ps1: ERROR: At Line: -2 char: 9
ERROR: + function
ERROR: + ~
ERROR: Missing name after function keyword.
ERROR:
SISA.Inventar.Run.ps1: ERROR: At Line: -2 char: 9
ERROR: + function
ERROR: + ~
ERROR: Missing name after function keyword.
ERROR: + CategoryInfo : ParserError: (:) [], ParseException
ERROR: + FullyQualifiedErrorId : MissingNameAfterKeyword
ERROR:

*** PowerShell Script finished. ***
>> Execution time: 00:00:01
>> Script Ended
And this is the generated 'SISA.Inventar.Run.ps1' File

Code: Select all

Using module .\Software\SoftwareCollector.psm1
Using module .\Hardware\HardwareCollector.psm1
Using module .\Helper\LogException.psm1
Using module .\Helper\Setup.psm1
Using module .\Helper\OutputFormatter.psm1
Using module ..\..\Helper\OutputFormatter.psm1
Using module ..\..\Helper\OutputFormatter.psm1
Using module ..\..\Helper\OutputFormatter.psm1
#------------------------------------------------------------------------
# Source File Information (DO NOT MODIFY)
# Source ID: c0439dc0-01c8-4f9a-aa3e-a43f1f38e416
# Source File: C:\DEV\SISA.Inventar\SISA.Inventar\SISA.Inventar.psproj
#------------------------------------------------------------------------
<#
.NOTES
--------------------------------------------------------------------------------
Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.154
Generated on: 30.08.2018 10:06
Generated by: pt
--------------------------------------------------------------------------------
.DESCRIPTION
Script generated by PowerShell Studio 2018
#>



#region Source: Startup.pss
#----------------------------------------------
#region Import Assemblies
#----------------------------------------------
#endregion Import Assemblies



function Main {

#Check the Environment
Try
{
$setupResult = Setup
If (!$setupResult)
{
Write-Error -Message ("The Root Folder" + $global:rootDir + "does not exist or is not accessible!") -Category ObjectNotFound
Exit
}
}
Catch
{
LogException $global:logfilename $_
Exit
}

#Read the Software
Try
{
CollectSoftware
ExportSoftwareList
WriteErrorLog
}
Catch
{
LogException $global:Slogfilename $_
}

#Read the Hardware
Try
{
CollectHardware
ExportHardwareList
WriteErrorLog
}
Catch
{
LogException $global:Hlogfilename $_
}



$script:ExitCode = 0
}




#endregion Source: Startup.pss

#region Source: Hardware\SpecificCollector\FirewallCollector.psm1
function
{



Function CollectFirewall
{
$prefix = "FireWall"

$domainprofile = netsh advfirewall show domainprofile state
$domainprofile = $domainprofile.GetValue(3).Remove(0, 5).Trim()
AddHardWare -code ($prefix + "DomainProfile") -value $domainprofile

$privateprofile = netsh advfirewall show privateprofile state
$privateprofile = $privateprofile.GetValue(3).Remove(0, 5).Trim()
AddHardWare -code ($prefix + "PrivateProfile") -value $privateprofile

$publicprofile = netsh advfirewall show publicprofile state
$publicprofile = $publicprofile.GetValue(3).Remove(0, 5).Trim()
AddHardWare -code ($prefix + "PublicProfile") -value $publicprofile
}

Export-ModuleMember CollectFirewall





}
#endregion Source: FirewallCollector.psm1

#region Source: Hardware\SpecificCollector\MachineCollector.psm1
function
{

$prefix = "Machine"

function CollectMachine
{
$win32ComputerSystem = Get-CimInstance -ClassName Win32_ComputerSystem

AddHardWare -code ($prefix + "Manufacturer") -value $win32ComputerSystem.Manufacturer
AddHardWare -code ($prefix + "Model") -value $win32ComputerSystem.Model
AddHardWare -code ($prefix + "Family") -value $win32ComputerSystem.SystemFamily
AddHardWare -code ($prefix + "Status") -value $win32ComputerSystem.Status
AddHardWare -code ($prefix + "DNSHostName") -value $win32ComputerSystem.DNSHostName
AddHardWare -code ($prefix + "Domain") -value $win32ComputerSystem.Domain
AddHardWare -code ($prefix + "Type") -value $win32ComputerSystem.SystemType
AddHardWare -code ($prefix + "User") -value $win32ComputerSystem.UserName

$cimBiosElement = Get-CimInstance -ClassName CIM_BIOSElement
AddHardWare -code ($prefix + "BiosVersion") -value $cimBiosElement.Caption
AddHardWare -code ($prefix + "BiosSerial") -value $cimBiosElement.SerialNumber

$cimDiskDrive = Get-CimInstance -ClassName CIM_DiskDrive

$x = 0
ForEach ($drive In $cimDiskDrive)
{

AddHardWare -code ($prefix + "DiskModel_" + $x) -value $drive.Model
AddHardWare -code ($prefix + "DiskSerial_" + $x) -value $drive.SerialNumber
AddHardWare -code ($prefix + "DiskInterface_" + $x) -value $drive.InterfaceType
AddHardWare -code ($prefix + "DiskSize_" + $x) -value $drive.Size

$x += 1
}

AddHardWare -code ($prefix + "DiskCount") -value $x

$win32VideoController = Get-CimInstance -ClassName Win32_VideoController

$x = 0
ForEach ($contoller In $win32VideoController)
{
AddHardWare -code ($prefix + "VideoController_" + $x) -value $contoller.Caption
AddHardWare -code ($prefix + "VideoControllerCurrentVerticalResolution_" + $x) -value $contoller.CurrentVerticalResolution
AddHardWare -code ($prefix + "VideoControllerCurrentHorizontalResolution_" + $x) -value $contoller.CurrentHorizontalResolution
AddHardWare -code ($prefix + "VideoControllerStatus_" + $x) -value $contoller.Status
$x += 1
}

AddHardWare -code ($prefix + "VideoControllerCount") -value $x

$win32PhysicalMemory = Get-CimInstance -ClassName Win32_PhysicalMemory

$x = 0
ForEach ($memry In $win32PhysicalMemory)
{
AddHardWare -code ($prefix + "MemoryBankLabel_" + $x) -value $memry.BankLabel
AddHardWare -code ($prefix + "MemoryCaption_" + $x) -value $memry.Caption
AddHardWare -code ($prefix + "MemoryManufacturer_" + $x) -value $memry.Manufacturer
AddHardWare -code ($prefix + "MemoryFormFactor_" + $x) -value $memry.FormFactor
AddHardWare -code ($prefix + "MemoryPartNumber_" + $x) -value $memry.PartNumber
AddHardWare -code ($prefix + "MemorySerialNumber_" + $x) -value $memry.SerialNumber
AddHardWare -code ($prefix + "MemoryCapacity_" + $x) -value $memry.Capacity

$x += 1
}

AddHardWare -code ($prefix + "MemoryCount") -value $x
}

Export-ModuleMember CollectMachine





}
#endregion Source: MachineCollector.psm1

#region Source: Hardware\SpecificCollector\OSCollector.psm1
function
{

$prefix = "OS"

function CollectOS
{
$cimsoftwareLicensingService = Get-CimInstance -ClassName SoftwareLicensingService
AddHardWare -code ($prefix + "LicenseKey") -value $cimsoftwareLicensingService.OA3xOriginalProductKey

$cimOS = Get-CimInstance -ClassName Win32_OperatingSystem
AddHardWare -code ($prefix + "Status") -value $cimOS.Status
AddHardWare -code ($prefix + "Caption") -value $cimOS.Caption
AddHardWare -code ($prefix + "CodeSet") -value $cimOS.CodeSet
AddHardWare -code ($prefix + "CountryCode") -value $cimOS.CountryCode
AddHardWare -code ($prefix + "Locale") -value $cimOS.Locale
AddHardWare -code ($prefix + "MUILanguages") -value $cimOS.MUILanguages
AddHardWare -code ($prefix + "OSArchitecture") -value $cimOS.OSArchitecture
AddHardWare -code ($prefix + "SerialNumber") -value $cimOS.SerialNumber
AddHardWare -code ($prefix + "Version") -value $cimOS.Version
AddHardWare -code ($prefix + "InstallDate") -value $cimOS.InstallDate
}

Export-ModuleMember CollectOS





}
#endregion Source: OSCollector.psm1

#region Source: Globals.ps1
function Invoke-Globals_ps1
{
$global:errorarray = @()

$global:Harray = @()

$global:Sarray = @()

$global:rootDir = "\\Sisanas06\all_sisa\Inventar\"
$global:SrootDir = "\\Sisanas06\all_sisa\Inventar\Software"
$global:HrootDir = "\\Sisanas06\all_sisa\Inventar\Hardware"
$global:LrootDir = "\\Sisanas06\all_sisa\Inventar\Log"

#general Log File
$global:logfilename = Join-Path -Path $global:LrootDir -ChildPath "${env:COMPUTERNAME}_$(get-date -f yyyyMMdd).log"

#Hardware Data and Log File
$global:Hfilename = Join-Path -Path $global:HrootDir -ChildPath "${env:COMPUTERNAME}_$(get-date -f yyyyMMdd).hwi"
$global:Hlogfilename = Join-Path -Path $global:LrootDir -ChildPath "${env:COMPUTERNAME}_$(get-date -f yyyyMMdd).hwi.log"

#Software Data and Log File
$global:Sfilename = Join-Path -Path $global:SrootDir -ChildPath "${env:COMPUTERNAME}_$(get-date -f yyyyMMdd).swi"
$global:Slogfilename = Join-Path -Path $global:LrootDir -ChildPath "${env:COMPUTERNAME}_$(get-date -f yyyyMMdd).swi.log"

}
#endregion Source: Globals.ps1

#Start the application
Main ($CommandLine)

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Beginner Question :-)

Post by jvierra » Thu Aug 30, 2018 1:15 am

You cannot "run" a module. You can create a test project that uses the modules. Also the main module folder has to have a module file that matches the name of the folder.

Locked