Asked  8 Months ago    Answers:  5   Viewed   5 times

I've started using PowerShell to get some things done and played with the variable $null in PowerShell.

I've encountered the problem that when I assign the variable $null to a variable defined in a class, the test returns false not true.

Example Code:

class test {

$test = [test]::new()

$test.test1 = $null

$null -eq $test2 # tests true

$null -eq $test.test1 # tests false

Now the test of the undefinded variable $test2 returns true as every undefined variable in PowerShell is assigned $null.

But if I test the property test1 of the object test which I assigned $null tests false for $null

Is it because in PowerShell $null is an object with the value $null and now the property of the object is not $null as it has the object $null assigned to it with an empty value?

I've read into the docs of Microsoft "Everything you wanted to know about $null", but it does not enlighten me.

If I do not initialize the variable, then it will test true for $null.



$test.test1 is not $null, it's empty string (because you explicitely defined it's value to be [string]):

C:> $test.test1.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


C:> $test.test1 -eq ""
Thursday, October 21, 2021

There were some great comments to the question, and I've spent some time investigating various ways to approach the problem.

To begin with, what I've initially asked for is not possible. I mean, if you go the module way, then the module should be physically present on a target machine to be able to Import-Module into remote session.

To abstract my question further, I'm trying to create a reusable PowerShell-based framework for the product deployments. It's going to be a push-manner deployments, meaning that we encourage people to run some scripts on a local machine to deploy to some remote server. As far as I investigated the area, there are two possible ways which are friendly to the common sense.

Modules approach

The process to follow:

  • place each logically different piece of functionality into the PowerShell module (*.psm1)
  • distribute the module to the remote machine and extend the PSModulePath variable to include the new modules location
  • on a client machine, create a new session to the remote server, and use Invoke-Command -Session $s -ScriptBlock {...}
  • in the script block start from Import-Module CustomModule - it will search the CustomModule on a remote machine and obviously will find it


The following are the reasons to love this approach for:

  • the consequence of the traditional module role - facilitate the creation of reusable libraries
  • according to the great book Windows PowerShell in Action, "modules can be used to create domain-specific applications". As far as I understand, it can be achieved by combining the module nesting and mixing script / binary modules to expose the intuitive interface specific to a certain domain. Basically, this is the one I value most for the goal of PowerShell-based deployment framework


The following is important to take into consideration:

  • You have to find a way to deliver the custom modules to the remote machine. I have played with NuGet, and I'm not sure it suits well for the task, but there are other options as well, for instance, MSI installer or plain xcopy from the shared folder. Besides, the delivery mechanism should support upgrade / downgrade and (preferably) multi-instance installations, but that's more related to my task than to the problem in general

Scripts approach

The process to follow:

  • place each logically different piece of functionality in a separate PowerShell script (*.ps1)
  • on a client machine, create a new session to the remote server, and use Invoke-Command -Session $s -FilePath .myscript.ps1 to load the functions defined in a script to the remote session
  • use another Invoke-Command -Session $s -ScriptBlock {...} and refer to your custom functions - they will be there in a session


The following are good points of this approach:

  • it is simple - you don't have to know about module peculiarities. Just write plain PowerShell scripts and that's it
  • you don't have to deliver anything to the remote machine - this makes the solution even simpler and less error-prone in maintenance


Sure, it's not ideal:

  • there's less control over the solution: for instance, if you "import" a set of functions to the session, all of them are "imported" and visible to the user, so no "encapsulation", etc. I'm sure many solutions can live with this, so don't decide based on this point only
  • the functionality in each file has to be self-contained - any dot-sourcing or module import from there will search the remote machine, not the local one

Finally, I should say that remote machine still needs to be prepared for the remoting. This is what I mean:

  • execution policy should be changed to something, because it is restricted by default: Set-ExecutionPolicy Unrestricted
  • PowerShell remoting should be enabled: Enable-PSRemoting
  • the account the script runs as should be added to the local administrators of the remote server
  • if you plan to access file shares in the remote session, make sure you are aware about multi-hop authentication and take proper actions
  • make sure your antivirus is your friend and doesn't send you to the PowerShell hell
Wednesday, July 28, 2021

I don't know why they output differently, but the message that we see from git push is coming over stderr. This means that they are both showing errors, although the ISE is making them much louder, and converting it into error objects.

Consider this output from the PowerShell prompt:

PS> git push
Everything up-to-date
PS> git push 2> $null    # Redirect stderr to $null
PS> $LastExitCode

and compare it to the ISE:

PS> git push
git : Everything up-to-date
At line:1 char:1
+ git push
+ ~~~~~~~~
    + CategoryInfo          : NotSpecified: (Everything up-to-date:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
PS> git push 2> $null
PS> $LastExitCode

Except for the extra output from the error object being displayed, the output is the same. The ISE has converted the stderr string to a NativeCommandError object, and it does even display the error message if you look past the red.

Sunday, September 26, 2021

You can use threading for this.
When you do a loop or wait for an input in kivy, the main thread is waiting, and nothing will update on the app. threading will prevent that.
Use threading to make another thread besides the main thread.

from import App
from kivy.uix.boxlayout import BoxLayout
from import StringProperty
from kivy.lang import Builder
import threading


    orientation: 'horizontal'
    cols: 2
        text: root.tobeupd
        text: 'Start Update'
        on_release: root.upd_ltxt()


class MyBox(BoxLayout):
    tobeupd = StringProperty()

    def __init__(self,*args,**kwargs):
        self.tobeupd = '#'

    def upd_ltxt(self):

    def update_label(self):
        for i in range(1,10):
            self.tobeupd = str(i)
            input('Write something: ')  # new line, see edit below

class updApp(App):
    def build(self):
        return MyBox()

if __name__ == '__main__':

Now its worth mentioning that you can keep pushing the button and start threads, even if the first did not finish yet. This might be an unwanted behavior.
This can be prevented by disabling the button in the beginning of the thread, and enabling it again at the end.

Give the button an id in kv:

    id: updatebutton
    text: 'Start Update'
    on_release: root.upd_ltxt()

And in the thread do like this:

def update_label(self):

    self.ids.updatebutton.disabled = True

    for i in range(1,10):
        self.tobeupd = str(i)
        input('Write something: ')

    self.ids.updatebutton.disabled = False
Thursday, November 4, 2021
5 is a function. To call a function, you have to put () after it:

Wednesday, November 17, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :