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

jvierra
Posts: 14157
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 36 times

jvierra
Posts: 14157
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.

jvierra
Posts: 14157
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 38 times

jvierra
Posts: 14157
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?

jvierra
Posts: 14157
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
  1. >> Running (SISA.Inventar.psproj) Project Script...
  2.  
  3. >> Building (SISA.Inventar) Project...
  4.  
  5. >> Merging 'Startup.pss' ...
  6.  
  7. >> Merging 'FirewallCollector.psm1' ...
  8.  
  9. >> Merging 'MachineCollector.psm1' ...
  10.  
  11. >> Merging 'OSCollector.psm1' ...
  12.  
  13. >> Merging 'Globals.ps1' ...
  14.  
  15. >> Writing 'SISA.Inventar.Run.ps1' ...
  16.  
  17. >> Platform: V5 64Bit (STA) (Elevated)
  18.  
  19. SISA.Inventar.Run.ps1: ERROR: At Line: -2 char: 9
  20.  
  21. ERROR: + function
  22.  
  23. ERROR: +         ~
  24.  
  25. ERROR: Missing name after function keyword.
  26.  
  27. ERROR:
  28.  
  29. SISA.Inventar.Run.ps1: ERROR: At Line: -2 char: 9
  30.  
  31. ERROR: + function
  32.  
  33. ERROR: +         ~
  34.  
  35. ERROR: Missing name after function keyword.
  36.  
  37. ERROR:
  38.  
  39. SISA.Inventar.Run.ps1: ERROR: At Line: -2 char: 9
  40.  
  41. ERROR: + function
  42.  
  43. ERROR: +         ~
  44.  
  45. ERROR: Missing name after function keyword.
  46.  
  47. ERROR:     + CategoryInfo          : ParserError: (:) [], ParseException
  48.  
  49. ERROR:     + FullyQualifiedErrorId : MissingNameAfterKeyword
  50.  
  51. ERROR:
  52.  
  53.  
  54.  
  55. *** PowerShell Script finished. ***
  56.  
  57. >> Execution time: 00:00:01
  58.  
  59. >> Script Ended
  60.  
  61.  
And this is the generated 'SISA.Inventar.Run.ps1' File
  1. Using module .\Software\SoftwareCollector.psm1
  2.  
  3. Using module .\Hardware\HardwareCollector.psm1
  4.  
  5. Using module .\Helper\LogException.psm1
  6.  
  7. Using module .\Helper\Setup.psm1
  8.  
  9. Using module .\Helper\OutputFormatter.psm1
  10.  
  11. Using module ..\..\Helper\OutputFormatter.psm1
  12.  
  13. Using module ..\..\Helper\OutputFormatter.psm1
  14.  
  15. Using module ..\..\Helper\OutputFormatter.psm1
  16.  
  17. #------------------------------------------------------------------------
  18.  
  19. # Source File Information (DO NOT MODIFY)
  20.  
  21. # Source ID: c0439dc0-01c8-4f9a-aa3e-a43f1f38e416
  22.  
  23. # Source File: C:\DEV\SISA.Inventar\SISA.Inventar\SISA.Inventar.psproj
  24.  
  25. #------------------------------------------------------------------------
  26.  
  27. <#
  28.  
  29.     .NOTES
  30.  
  31.     --------------------------------------------------------------------------------
  32.  
  33.      Code generated by:  SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.154
  34.  
  35.      Generated on:       30.08.2018 10:06
  36.  
  37.      Generated by:       pt
  38.  
  39.     --------------------------------------------------------------------------------
  40.  
  41.     .DESCRIPTION
  42.  
  43.         Script generated by PowerShell Studio 2018
  44.  
  45. #>
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. #region Source: Startup.pss
  54.  
  55. #----------------------------------------------
  56.  
  57. #region Import Assemblies
  58.  
  59. #----------------------------------------------
  60.  
  61. #endregion Import Assemblies
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. function Main {
  70.  
  71.    
  72.  
  73.     #Check the Environment
  74.  
  75.     Try
  76.  
  77.     {
  78.  
  79.         $setupResult = Setup
  80.  
  81.         If (!$setupResult)
  82.  
  83.         {
  84.  
  85.             Write-Error -Message ("The Root Folder" + $global:rootDir + "does not exist or is not accessible!") -Category ObjectNotFound
  86.  
  87.             Exit
  88.  
  89.         }
  90.  
  91.     }
  92.  
  93.     Catch
  94.  
  95.     {
  96.  
  97.         LogException $global:logfilename $_
  98.  
  99.         Exit
  100.  
  101.     }
  102.  
  103.    
  104.  
  105.     #Read the Software
  106.  
  107.     Try
  108.  
  109.     {
  110.  
  111.         CollectSoftware
  112.  
  113.         ExportSoftwareList
  114.  
  115.         WriteErrorLog
  116.  
  117.     }
  118.  
  119.     Catch
  120.  
  121.     {
  122.  
  123.         LogException $global:Slogfilename $_
  124.  
  125.     }
  126.  
  127.    
  128.  
  129.     #Read the Hardware
  130.  
  131.     Try
  132.  
  133.     {
  134.  
  135.         CollectHardware
  136.  
  137.         ExportHardwareList
  138.  
  139.         WriteErrorLog
  140.  
  141.     }
  142.  
  143.     Catch
  144.  
  145.     {
  146.  
  147.         LogException $global:Hlogfilename $_
  148.  
  149.     }
  150.  
  151.    
  152.  
  153.  
  154.  
  155.    
  156.  
  157.     $script:ExitCode = 0
  158.  
  159. }
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169. #endregion Source: Startup.pss
  170.  
  171.  
  172.  
  173. #region Source: Hardware\SpecificCollector\FirewallCollector.psm1
  174.  
  175. function
  176.  
  177. {
  178.  
  179.    
  180.  
  181.    
  182.  
  183.    
  184.  
  185.     Function CollectFirewall
  186.  
  187.     {
  188.  
  189.         $prefix = "FireWall"
  190.  
  191.        
  192.  
  193.         $domainprofile = netsh advfirewall show domainprofile state
  194.  
  195.         $domainprofile = $domainprofile.GetValue(3).Remove(0, 5).Trim()
  196.  
  197.         AddHardWare -code ($prefix + "DomainProfile") -value $domainprofile
  198.  
  199.        
  200.  
  201.         $privateprofile = netsh advfirewall show privateprofile state
  202.  
  203.         $privateprofile = $privateprofile.GetValue(3).Remove(0, 5).Trim()
  204.  
  205.         AddHardWare -code ($prefix + "PrivateProfile") -value $privateprofile
  206.  
  207.        
  208.  
  209.         $publicprofile = netsh advfirewall show publicprofile state
  210.  
  211.         $publicprofile = $publicprofile.GetValue(3).Remove(0, 5).Trim()
  212.  
  213.         AddHardWare -code ($prefix + "PublicProfile") -value $publicprofile
  214.  
  215.     }
  216.  
  217.    
  218.  
  219.     Export-ModuleMember CollectFirewall
  220.  
  221.    
  222.  
  223.    
  224.  
  225.    
  226.  
  227.    
  228.  
  229.    
  230.  
  231. }
  232.  
  233. #endregion Source: FirewallCollector.psm1
  234.  
  235.  
  236.  
  237. #region Source: Hardware\SpecificCollector\MachineCollector.psm1
  238.  
  239. function
  240.  
  241. {
  242.  
  243.    
  244.  
  245.     $prefix = "Machine"
  246.  
  247.    
  248.  
  249.     function CollectMachine
  250.  
  251.     {
  252.  
  253.         $win32ComputerSystem = Get-CimInstance -ClassName Win32_ComputerSystem
  254.  
  255.        
  256.  
  257.         AddHardWare -code ($prefix + "Manufacturer") -value $win32ComputerSystem.Manufacturer
  258.  
  259.         AddHardWare -code ($prefix + "Model") -value $win32ComputerSystem.Model
  260.  
  261.         AddHardWare -code ($prefix + "Family") -value $win32ComputerSystem.SystemFamily
  262.  
  263.         AddHardWare -code ($prefix + "Status") -value $win32ComputerSystem.Status
  264.  
  265.         AddHardWare -code ($prefix + "DNSHostName") -value $win32ComputerSystem.DNSHostName
  266.  
  267.         AddHardWare -code ($prefix + "Domain") -value $win32ComputerSystem.Domain
  268.  
  269.         AddHardWare -code ($prefix + "Type") -value $win32ComputerSystem.SystemType
  270.  
  271.         AddHardWare -code ($prefix + "User") -value $win32ComputerSystem.UserName
  272.  
  273.        
  274.  
  275.         $cimBiosElement = Get-CimInstance -ClassName CIM_BIOSElement
  276.  
  277.         AddHardWare -code ($prefix + "BiosVersion") -value $cimBiosElement.Caption
  278.  
  279.         AddHardWare -code ($prefix + "BiosSerial") -value $cimBiosElement.SerialNumber
  280.  
  281.        
  282.  
  283.         $cimDiskDrive = Get-CimInstance -ClassName CIM_DiskDrive
  284.  
  285.        
  286.  
  287.         $x = 0
  288.  
  289.         ForEach ($drive In $cimDiskDrive)
  290.  
  291.         {
  292.  
  293.            
  294.  
  295.             AddHardWare -code ($prefix + "DiskModel_" + $x) -value $drive.Model
  296.  
  297.             AddHardWare -code ($prefix + "DiskSerial_" + $x) -value $drive.SerialNumber
  298.  
  299.             AddHardWare -code ($prefix + "DiskInterface_" + $x) -value $drive.InterfaceType
  300.  
  301.             AddHardWare -code ($prefix + "DiskSize_" + $x) -value $drive.Size
  302.  
  303.            
  304.  
  305.             $x += 1
  306.  
  307.         }
  308.  
  309.        
  310.  
  311.         AddHardWare -code ($prefix + "DiskCount") -value $x
  312.  
  313.        
  314.  
  315.         $win32VideoController = Get-CimInstance -ClassName Win32_VideoController
  316.  
  317.        
  318.  
  319.         $x = 0
  320.  
  321.         ForEach ($contoller In $win32VideoController)
  322.  
  323.         {
  324.  
  325.             AddHardWare -code ($prefix + "VideoController_" + $x) -value $contoller.Caption
  326.  
  327.             AddHardWare -code ($prefix + "VideoControllerCurrentVerticalResolution_" + $x) -value $contoller.CurrentVerticalResolution
  328.  
  329.             AddHardWare -code ($prefix + "VideoControllerCurrentHorizontalResolution_" + $x) -value $contoller.CurrentHorizontalResolution
  330.  
  331.             AddHardWare -code ($prefix + "VideoControllerStatus_" + $x) -value $contoller.Status
  332.  
  333.             $x += 1
  334.  
  335.         }
  336.  
  337.        
  338.  
  339.         AddHardWare -code ($prefix + "VideoControllerCount") -value $x
  340.  
  341.        
  342.  
  343.         $win32PhysicalMemory = Get-CimInstance -ClassName Win32_PhysicalMemory
  344.  
  345.        
  346.  
  347.         $x = 0
  348.  
  349.         ForEach ($memry In $win32PhysicalMemory)
  350.  
  351.         {
  352.  
  353.             AddHardWare -code ($prefix + "MemoryBankLabel_" + $x) -value $memry.BankLabel
  354.  
  355.             AddHardWare -code ($prefix + "MemoryCaption_" + $x) -value $memry.Caption
  356.  
  357.             AddHardWare -code ($prefix + "MemoryManufacturer_" + $x) -value $memry.Manufacturer
  358.  
  359.             AddHardWare -code ($prefix + "MemoryFormFactor_" + $x) -value $memry.FormFactor
  360.  
  361.             AddHardWare -code ($prefix + "MemoryPartNumber_" + $x) -value $memry.PartNumber
  362.  
  363.             AddHardWare -code ($prefix + "MemorySerialNumber_" + $x) -value $memry.SerialNumber
  364.  
  365.             AddHardWare -code ($prefix + "MemoryCapacity_" + $x) -value $memry.Capacity
  366.  
  367.            
  368.  
  369.             $x += 1
  370.  
  371.         }
  372.  
  373.        
  374.  
  375.         AddHardWare -code ($prefix + "MemoryCount") -value $x
  376.  
  377.     }
  378.  
  379.    
  380.  
  381.     Export-ModuleMember CollectMachine
  382.  
  383.    
  384.  
  385.    
  386.  
  387.    
  388.  
  389.    
  390.  
  391.    
  392.  
  393. }
  394.  
  395. #endregion Source: MachineCollector.psm1
  396.  
  397.  
  398.  
  399. #region Source: Hardware\SpecificCollector\OSCollector.psm1
  400.  
  401. function
  402.  
  403. {
  404.  
  405.    
  406.  
  407.     $prefix = "OS"
  408.  
  409.    
  410.  
  411.     function CollectOS
  412.  
  413.     {
  414.  
  415.         $cimsoftwareLicensingService = Get-CimInstance -ClassName SoftwareLicensingService
  416.  
  417.         AddHardWare -code ($prefix + "LicenseKey") -value $cimsoftwareLicensingService.OA3xOriginalProductKey
  418.  
  419.        
  420.  
  421.         $cimOS = Get-CimInstance -ClassName Win32_OperatingSystem
  422.  
  423.         AddHardWare -code ($prefix + "Status") -value $cimOS.Status
  424.  
  425.         AddHardWare -code ($prefix + "Caption") -value $cimOS.Caption
  426.  
  427.         AddHardWare -code ($prefix + "CodeSet") -value $cimOS.CodeSet
  428.  
  429.         AddHardWare -code ($prefix + "CountryCode") -value $cimOS.CountryCode
  430.  
  431.         AddHardWare -code ($prefix + "Locale") -value $cimOS.Locale
  432.  
  433.         AddHardWare -code ($prefix + "MUILanguages") -value $cimOS.MUILanguages
  434.  
  435.         AddHardWare -code ($prefix + "OSArchitecture") -value $cimOS.OSArchitecture
  436.  
  437.         AddHardWare -code ($prefix + "SerialNumber") -value $cimOS.SerialNumber
  438.  
  439.         AddHardWare -code ($prefix + "Version") -value $cimOS.Version
  440.  
  441.         AddHardWare -code ($prefix + "InstallDate") -value $cimOS.InstallDate
  442.  
  443.     }
  444.  
  445.    
  446.  
  447.     Export-ModuleMember CollectOS
  448.  
  449.    
  450.  
  451.    
  452.  
  453.    
  454.  
  455.    
  456.  
  457.    
  458.  
  459. }
  460.  
  461. #endregion Source: OSCollector.psm1
  462.  
  463.  
  464.  
  465. #region Source: Globals.ps1
  466.  
  467. function Invoke-Globals_ps1
  468.  
  469. {
  470.  
  471.     $global:errorarray = @()
  472.  
  473.    
  474.  
  475.     $global:Harray = @()
  476.  
  477.    
  478.  
  479.     $global:Sarray = @()
  480.  
  481.    
  482.  
  483.     $global:rootDir = "\\Sisanas06\all_sisa\Inventar\"
  484.  
  485.     $global:SrootDir = "\\Sisanas06\all_sisa\Inventar\Software"
  486.  
  487.     $global:HrootDir = "\\Sisanas06\all_sisa\Inventar\Hardware"
  488.  
  489.     $global:LrootDir = "\\Sisanas06\all_sisa\Inventar\Log"
  490.  
  491.    
  492.  
  493.     #general Log File
  494.  
  495.     $global:logfilename = Join-Path -Path $global:LrootDir -ChildPath "${env:COMPUTERNAME}_$(get-date -f yyyyMMdd).log"
  496.  
  497.    
  498.  
  499.     #Hardware Data and Log File
  500.  
  501.     $global:Hfilename = Join-Path -Path $global:HrootDir -ChildPath "${env:COMPUTERNAME}_$(get-date -f yyyyMMdd).hwi"
  502.  
  503.     $global:Hlogfilename = Join-Path -Path $global:LrootDir -ChildPath "${env:COMPUTERNAME}_$(get-date -f yyyyMMdd).hwi.log"
  504.  
  505.    
  506.  
  507.     #Software Data and Log File
  508.  
  509.     $global:Sfilename = Join-Path -Path $global:SrootDir -ChildPath "${env:COMPUTERNAME}_$(get-date -f yyyyMMdd).swi"
  510.  
  511.     $global:Slogfilename = Join-Path -Path $global:LrootDir -ChildPath "${env:COMPUTERNAME}_$(get-date -f yyyyMMdd).swi.log"
  512.  
  513.    
  514.  
  515. }
  516.  
  517. #endregion Source: Globals.ps1
  518.  
  519.  
  520.  
  521. #Start the application
  522.  
  523. Main ($CommandLine)

jvierra
Posts: 14157
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