Reading a null value for TextBox.Text.

Hello!

I am trying to make a little automated program to make some changes to start services. I am using a button click event to run the following:

{
    
    #Define Variables
    $ComputerName = $WorkstationNameTextBox.Text
    $Credentials = $ServiceIDTextBox.Text
    $ServiceName = [ToString]$ServiceNameTextBox.Text

    #create session and set startup type
    $s = New-PSSession -ComputerName $ComputerName -Credential $Credentials
    Invoke-Command -Session $s -ScriptBlock {Set-Service -Name $ServiceName -StartupType Automatic

    #changing the Failure options
    sc.exe failure $ServiceName actions = restart/60000/restart/60000/restart/60000 reset= 86400;

    #Restart service
    Restart-Service -Name $ServiceName -Force -ErrorAction Stop}

Open in new window


I am getting an error with this script:

Cannot bind argument to parameter 'Name' because it is null.
    + CategoryInfo          : InvalidData: (:) [Set-Service], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetServiceCommand
    + PSComputerName        : MyComputerName
 
Cannot bind argument to parameter 'Name' because it is null.
    + CategoryInfo          : InvalidData: (:) [Restart-Service], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RestartServiceCommand
    + PSComputerName        : MyComputerName

I am sure this is something small, but I cannot figure it out. Any Ideas as to why this is happening? And how to fix my issue.

Thank you!,
Shannon
LVL 2
ShannonCallahanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Two things jump out:
Line 6:  [ToString] is not a valid type. Just drop it.
Then your Invoke-Command: the remote machine knows exactly nothing about your variables; you need to pass them as positional arguments to the scriptblock.
And always remove sessions you explicitly created.
	#Define Variables
	$ComputerName = $WorkstationNameTextBox.Text
	$Credentials = $ServiceIDTextBox.Text
	$ServiceName = $ServiceNameTextBox.Text

	#create session and set startup type
	$s = New-PSSession -ComputerName $ComputerName -Credential $Credentials
	Invoke-Command -Session $s -ArgumentList $ServiceName -ScriptBlock {
		Param($ServiceName)
		Set-Service -Name $ServiceName -StartupType Automatic
		#changing the Failure options
		sc.exe failure $ServiceName actions = restart/60000/restart/60000/restart/60000 reset= 86400;
		#Restart service
		Restart-Service -Name $ServiceName -Force -ErrorAction Stop
	}
	Remove-PSSession $s

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShannonCallahanAuthor Commented:
Sorry for the delayed response. Thank you very much!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.