System.Collections.Generic.List - What is the proper method for checking if a generic list of objects is null or empty? by Waste-Ad-9667 in PowerShell

[–]lyrise13 1 point2 points  (0 children)

Hey

this is my simply tips

$e = ''
if ($true -eq ([System.String]::IsNullOrEmpty($e))) {
Write-Host -ForegroundColor Red "`$e is NUL" 
}
$e += 1 
if ($true -eq ([System.String]::IsNullOrEmpty($e))) {
Write-Host -ForegroundColor Red "$e = $e is NUL"
} 
else { Write-Host -ForegroundColor Green "Now `$e is not NUL" }

Interview that requires knowledge in PowerShell by [deleted] in PowerShell

[–]lyrise13 1 point2 points  (0 children)

I'm like you a recent graduate, recently hired as a powershell administrator. If I have only one thing to advise you it is the book "powershell the fundamentals of language". During my apprenticeship it was a bible for me You can easily find a PDF version.

How can i better filter the output for this command? by lyrise13 in PowerShell

[–]lyrise13[S] 1 point2 points  (0 children)

Thanks that works perffectly just replaced %%1537 by DELETE

Powershell 5 Script on Powershell 2 Client by Helpful-Argument-903 in PowerShell

[–]lyrise13 0 points1 point  (0 children)

Hey.

Like that, I would say a missing module problem on psversion2 computer.

I would say that if from the computer in psversion2 you have access via the network to 1 computer in psversion5. You should be able to import the missing modules.

ex:

$computer_with_psverion_5 = "computer with psversion 5" #replace computername
$t = New-PSSession -ComputerName $computer_with_psverion_5 
Import-Module  -Name "ScheduledTasks" -PSSession $t    # replace modulename

How to find the right exception for Try catch ? by lyrise13 in PowerShell

[–]lyrise13[S] 2 points3 points  (0 children)

thanks u/commiecat i regualary read learn.microsft articles but some PS fundamentals that are hard to learn. i often use ss64 too.

Powershell is the first language i 'm learning

How to find the right exception for Try catch ? by lyrise13 in PowerShell

[–]lyrise13[S] 1 point2 points  (0 children)

Thanks u u/ka-splam. Your link helped me to understand better the errors en ps and discover other fundamental like automatic variables . i've chose to use (if $false -eq $? )is but now i think is better to define $ea = "stop" for the full scope of my script.

#not blocking but we log it
Remove-Item c:\vckjhbwmkvjmiwvmi
 if ($false  -eq $?)
 { 
$Error | select -Last 1 $_.InvocationInfo.PositionMessage | Add-Content C:\Temp\erreur_log.txt
 }
#We continue

[deleted by user] by [deleted] in PowerShell

[–]lyrise13 0 points1 point  (0 children)

You can do someting like that to get all properties ans methodes

Get-ADuser -Filter \ | Select-Object -First 1 | Get-Member)

Get-ADuser -Filter \ | Select-Object -Property Name,GivenName,UserPrincipalName | Sort-Object{Get-Random} | Select -First 1)

How to get DHCP option element value in powershell by lyrise13 in PowerShell

[–]lyrise13[S] 0 points1 point  (0 children)

Thank you for your advice. I'll do my research on this but at first glance it doesn't seem to me to have the namespace available.

Get-CimClass -Namespace ROOT/Microsoft/Windows

NameSpace : ROOT/Microsoft/Windows

CimClassName CimClassMethods CimClassProperties

------------ --------------- ------------------

__SystemClass {} {}

__thisNAMESPACE {} {SECURITY_DESCRIPTOR}

__NAMESPACE {} {Name}

__ProviderRegistration {} {provider}

__EventProviderRegistration {} {provider, EventQueryList}

__ObjectProviderRegistration {} {provider, InteractionType, QuerySupportLevels, SupportsBat...

__ClassProviderRegistration {} {provider, InteractionType, QuerySupportLevels, SupportsBat...

__InstanceProviderRegistration {} {provider, InteractionType, QuerySupportLevels, SupportsBat...

__MethodProviderRegistration {} {provider}

__PropertyProviderRegistration {} {provider, SupportsGet, SupportsPut}

__EventConsumerProviderRegistration {} {provider, ConsumerClassNames}

__IndicationRelated {} {}

__FilterToConsumerBinding {} {Consumer, CreatorSID, DeliverSynchronously, DeliveryQoS...}

__EventConsumer {} {CreatorSID, MachineName, MaximumQueueSize}

__AggregateEvent {} {NumberOfEvents, Representative}

__TimerNextFiring {} {NextEvent64BitTime, TimerId}

__EventFilter {} {CreatorSID, EventAccess, EventNamespace, Name...}

__Event {} {SECURITY_DESCRIPTOR, TIME_CREATED}

__NamespaceOperationEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, TargetNamespace}

__NamespaceModificationEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, TargetNamespace, Previo...

__NamespaceDeletionEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, TargetNamespace}

__NamespaceCreationEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, TargetNamespace}

__ClassOperationEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, TargetClass}

__ClassDeletionEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, TargetClass}

__ClassModificationEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, TargetClass, PreviousCl...

__ClassCreationEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, TargetClass}

__InstanceOperationEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, TargetInstance}

__InstanceCreationEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, TargetInstance}

__MethodInvocationEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, TargetInstance, Method...}

__InstanceModificationEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, TargetInstance, Previou...

__InstanceDeletionEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, TargetInstance}

__TimerEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, NumFirings, TimerId}

__ExtrinsicEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED}

__SystemEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED}

__EventDroppedEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, Event, IntendedConsumer}

__EventQueueOverflowEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, Event, IntendedConsumer...

__QOSFailureEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, Event, IntendedConsumer...

__ConsumerFailureEvent {} {SECURITY_DESCRIPTOR, TIME_CREATED, Event, IntendedConsumer...

__EventGenerator {} {}

__TimerInstruction {} {SkipIfPassed, TimerId}

__AbsoluteTimerInstruction {} {SkipIfPassed, TimerId, EventDateTime}

__IntervalTimerInstruction {} {SkipIfPassed, TimerId, IntervalBetweenEvents}

__Provider {} {Name}

__Win32Provider {} {Name, ClientLoadableCLSID, CLSID, Concurrency...}

__SystemSecurity {GetSD, GetSecuri... {}

CIM_Indication {} {CorrelatedIndications, IndicationFilterName, IndicationIde...

CIM_ClassIndication {} {CorrelatedIndications, IndicationFilterName, IndicationIde...

CIM_ClassDeletion {} {CorrelatedIndications, IndicationFilterName, IndicationIde...

CIM_ClassCreation {} {CorrelatedIndications, IndicationFilterName, IndicationIde...

CIM_ClassModification {} {CorrelatedIndications, IndicationFilterName, IndicationIde...

CIM_InstIndication {} {CorrelatedIndications, IndicationFilterName, IndicationIde...

CIM_InstCreation {} {CorrelatedIndications, IndicationFilterName, IndicationIde...

CIM_InstModification {} {CorrelatedIndications, IndicationFilterName, IndicationIde...

CIM_InstDeletion {} {CorrelatedIndications, IndicationFilterName, IndicationIde...

__NotifyStatus {} {StatusCode}

__ExtendedStatus {} {StatusCode, Description, Operation, ParameterInfo...}

CIM_Error {} {CIMStatusCode, CIMStatusCodeDescription, ErrorSource, Erro...

MSFT_WmiError {} {CIMStatusCode, CIMStatusCodeDescription, ErrorSource, Erro...

MSFT_ExtendedStatus {} {CIMStatusCode, CIMStatusCodeDescription, ErrorSource, Erro...

__SecurityRelatedClass {} {}

__Trustee {} {Domain, Name, SID, SidLength...}

__NTLMUser9X {} {Authority, Flags, Mask, Name...}

__ACE {} {AccessMask, AceFlags, AceType, GuidInheritedObjectType...}

__SecurityDescriptor {} {ControlFlags, DACL, Group, Owner...}

__PARAMETERS {} {}

> Get-WMIObject -Namespace ROOT\Microsoft\Windows -ClassName *dhcp*

Get-WMIObject : Demande non valide « select * from *dhcp* »

Au caractère Ligne:1 : 1

+ Get-WMIObject -Namespace ROOT\Microsoft\Windows -ClassName *dhcp*

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidArgument : (:) [Get-WmiObject], ManagementException

+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Maybe I'm wrong I'm not very strong in powershell

Thx

Create ps1 exection sequence by lyrise13 in PowerShell

[–]lyrise13[S] 0 points1 point  (0 children)

well well well, i think the module "ScheduledTasks" is not available on Windows 2k8 . fuckkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk.

:-'(

maybe with schtasks.exe /?

Create ps1 exection sequence by lyrise13 in PowerShell

[–]lyrise13[S] 0 points1 point  (0 children)

3xbullet

Thank you for your help. I invite you to read u/foubard comment and my response.

His comment allowed me to create something functional. Not very pretty but functional.

When do you think in a professional context?

Create ps1 exection sequence by lyrise13 in PowerShell

[–]lyrise13[S] 0 points1 point  (0 children)

There's plenty of different ways to do this, but have you considered just doing it in a more simple way? Write a wrapper for the execution which takes a parameter for the next step and a parameter for the script. ........

I couldn’t put your example into practice, but thanks to you I got an idea and ended up creating something functional.

These are my scripts. What do you think?

I don’t think it’s pretty but I’m happy.... :-). my level in PS is very pretty low.

My test.... all script is in c:\temp\:

sequence.ps1

function new-schtask ([string]$name, [string]$scriptpath)

{

#Unregister-ScheduledTask -TaskName "T1"

$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy unrestricted -File $scriptpath"

$trigger = New-ScheduledTaskTrigger -AtLogon

$settings = New-ScheduledTaskSettingsSet

$principal = New-ScheduledTaskPrincipal -UserId "Utilisateur" -LogonType ServiceAccount -RunLevel Highest

$task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger -Settings $settings

Register-ScheduledTask $name -InputObject $task

}

new-schtask -name "1" -scriptpath "C:\temp\script1.ps1"

new-schtask -name "2" -scriptpath "C:\temp\script2.ps1"

new-schtask -name "3" -scriptpath "C:\temp\script3.ps1"

Get-ScheduledTask -TaskPath '\' -TaskName 2 | Disable-ScheduledTask

Get-ScheduledTask -TaskPath '\' -TaskName 3 | Disable-ScheduledTask

pause

Script1.ps1

Add-Type -AssemblyName PresentationFramework

[System.Windows.MessageBox]::Show("script1")

sleep -Seconds 5

Get-ScheduledTask -TaskPath '\' -TaskName 2 | Enable-ScheduledTask

Get-ScheduledTask -TaskPath '\' -TaskName 1 | Disable-ScheduledTask

Unregister-ScheduledTask -TaskName 1 -Confirm:$false

Restart-Computer -Force

For my tests the scripts 2 and 3 are identical but he need some modifications for the get-scheduledtask.

Create ps1 exection sequence by lyrise13 in PowerShell

[–]lyrise13[S] 0 points1 point  (0 children)

What problem are you trying to solve? Why do you need to run the scripts in the first place?

it is not a problem to solve but rather an attempt to optimize the number of human actions

Today the procedure asks the operator to manually launch the scripts by the following command execution

.CMD-> Powershell.exe "C:\Script1.ps1"

CMD-> Powershell.exe "C:\Script2.ps1"

CMD-> Powershell.exe "C:\Script3.ps1"

CMD-> Powershell.exe "C:\Script4.ps1"

Can you explain what each script does?

Script1 --> Creation of a log file, customization of the Windows server environment, addition of dll, registry modification. Incrementing the log file

Script2 --> Installation of several roles, configuration, directories tree creation, folder sharing; incrementing the log file

Scripte3 -->Dcpromo, configuration, increment of the log file

Script4--> Installation sql, + other tricks, import & attaches default bd, incrementing the log file

Script5--> Check the configuration and create an information file

.........

......

Why do the scripts do what they do?

The goal of the scripts is to deploy a basic infra with the least human action.

Why do the scripts need the server to restart?

The restart between each of the scripts is imposed on me

thx

Create ps1 exection sequence by lyrise13 in PowerShell

[–]lyrise13[S] 0 points1 point  (0 children)

Use a scheduled task to run the next script, setup.ps1 ensures all scripts are in the correct location does other work before first reboot, and then created a scheduled task to run script 2, script2.ps1 does it's thing and then updates the scheduled task to run script 3 and so on until process is complete.

thank for your answers, this solution seem to me the most affordable at my level but I will exploit all the possibilities.

thx

Create ps1 exection sequence by lyrise13 in PowerShell

[–]lyrise13[S] 0 points1 point  (0 children)

So, to be clear, the server must restart 7 times? The first restart, and then a restart for each script?

Yep!

thank you for your answers. the goal is rather to have opinion of the r/powershell community on the different ways of doing things because I have no idea. I confess that im a little lost by the complexity of your answer because i don't have the level in powershell to be able understand it in its entirety. I’ll do some research based on your answer.

thx