Ok, there are a couple of issues with your script
1. If the NIC also has IPv6 the grep 'inet' will return both, hence the change to grep 'inet ' (blank after inet).
This cuases the inet6 to be ignored
2. By default the stderr stream is not returned, only the stdout.
To see any errors from the nsupdate command you need to redirect the stderr to stdout
3. This script was most probably created on a Windows box, and the lines are ended with a <CR><LF>
This will cause problems on a Linux box where only <LF> is used.
Hence the removal of all <CR> in the $script variable, effectively only using <LF> at the end of each line.
With these changes, the code works on my test Linux box.
Be aware that depending on the configuration of your DNS servers you might get an error "update failed: REFUSED".
This has nothing to do with the script, it's due to the configuration of your DNS server(s)
$script = @'
fqdn=`hostname`
ipaddress=`/sbin/ifconfig ens160 | grep 'inet ' | awk '{print $2}' | sed -e s/.*://`
mydnsserver=$(nslookup -type=soa $(hostname -d) | grep origin | awk -F'= ' '{print $2}')
echo "server ${mydnsserver}
update delete ${fqdn}.mydomain.com A
update add ${fqdn}.mydomain.com 3600 IN A ${ipaddress}
send
quit
" | nsupdate 2>&1
'@
$serv = "192.168.1.100"
$Username = 'root'
$pass = ConvertTo-SecureString -AsPlainText 'password' -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $pass
$session = New-SSHSession $serv -Credential $Cred -AcceptKey
$connected = $session.Connected
$result = Invoke-SSHCommand -SSHSession $session -Command $script.replace("`r", '') -Verbose
$result.Output
Remove-SSHSession $session -Verbose | Out-Null