简体   繁体   中英

Why am I getting a “not a cmdlet” error from inside a here-string?

Below is an extract of my html code, defined as $html in my Powershell script:

$html = @"
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <script type="text/javascript" src="/js/lib/dummy.js"></script>

  <link rel="stylesheet" type="text/css" href="/css/result-light.css">
  <link rel="stylesheet" type="text/css" href="https://rawgit.com/pguso/jquery-plugin-circliful/1.0.2/css/jquery.circliful.css">
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://rawgit.com/pguso/jquery-plugin-circliful/1.0.2/js/jquery.circliful.js"></script>
    <style type="text/css"></style>

    <title>Orders</title>

    </head>
        <body>
            <div class="container">
                <h3 style="text-align: center; margin-top: 2%; font-size: 300%">Actual Vs Expected orders for Today:</h3>
                <h2 style="text-align: center; color: #ff3333; margin-top: 2.5%; font-size: 500%">$SAPTodayExel<small style="color: #000000">/$ExpectedExelOrders</small></h2>                              
                <div class="col-lg-12">
                    <div id="test-circle"></div>
                    <table style="margin-top: 100px; width:75%; position: fixed; bottom: 40px;">

 <tr>
    <th style="font-size: 16px; width:11%; background: linear-gradient(to bottom,  #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">12</th>
    <td style="font-size: 30px; color: #ff3333; width:20%; background: linear-gradient(to bottom, #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">$SAPTodayNonConExel</td>
    <th style="font-size: 16px; width:8%; background: linear-gradient(to bottom,  #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">13</th>
    <td style="font-size: 30px; color: #ff3333; width:20%; background: linear-gradient(to bottom, #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">$SAPTodayPOSExel</td>
    <th style="font-size: 16px; width:8%; background: linear-gradient(to bottom,  #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">24</th>
    <td style="font-size: 30px; color: #ff3333; width:15%; background: linear-gradient(to bottom, #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">$SAPTodayROIExel</td>
    <th style="font-size: 16px; width:7%; background: linear-gradient(to bottom,  #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">245</th>
    <td style="font-size: 30px; color: #ff3333; width:20%; background: linear-gradient(to bottom, #f0f9ff 0%,#cbebff 49%,#a1dbff 100%)">$SAPTodayUnited</td>    
  </tr>
</table>
                </div>
            </div>
    </head>

<script type='text/javascript'>
        window.onload=function(){
        $( document ).ready(function() { // 6,32 5,38 2,34
            $("#test-circle").circliful({
            foregroundColor: "#cb60b3",
            backgroundColor: "#e5e7e9",
            pointColor: "none",
            fillColor: 'none',
            foregroundBorderWidth: 15,
            backgroundBorderWidth: 15,
            pointSize: 28.5,
            fontColor: '#aaa',

            animation: 1,
            animationStep: 5,

            showPercent: 1,
            noPercentageSign: false,
            replacePercentageByText: null,
            percentageTextSize: 22,         
            percent: $PercentReceived,
            multiPercentage: 0,
            percentages: null,

            targetPercent: null,
            targetTextSize: 12,
            targetColor: '#2980B9',

            icon: 'none',
            iconSize: '30',
            iconColor: '#ccc',
            iconPosition: 'top',

            target: 0,
            start: 0,

            textBelow: true,            
            text: null,
            textStyle: "font-size: 10px",
            textColor: '#17202a',
            textAdditionalCss: 'test',

            halfCircle: false,
            animateInView: false,
            decimals: 0,
            alwaysDecimals: false
            });
        });
    }
        </script>
</html>
"@

$html | out-file "c:\test.html"

When I run this as an html file, it runs perfectly normal and the script is running as expected. However, if I parse this via PS it comes up with an error:

document : The term 'document' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:46 char:6 + $( document ).ready(function() { // 6,32 5,38 2,34 + ~~~~~~~~ + CategoryInfo : ObjectNotFound: (document:String) [], CommandNot FoundException + FullyQualifiedErrorId : CommandNotFoundException

I have tried changing "$( document )" to "$("document"), adding in quotations, which seems to work, but when you look at the html file on output, it removes the "$(" and ")", so it effectively disables the script from enabling due to incorrect syntax.

Is it because it is seeing the dollar sign as a PS variable? How do I go around this?

Thanks

The problem line, as you know, has this

 $( document ) 

That is syntax for PowerShell subexpressions $() . PowerShell is telling you that "document" is not a command. Use backticks to escape that "$" in PowerShell so it wont treat it special

`$( document )

You have a similar issue with the following line that might not be as obvious

 $("#test-circle").circliful({ 

looks like this in your here string once processed

#test-circle.circliful({

You need to escape that dollar sign as well.


You could have just used a herestring with single quotes ( @' '@ ) and that would have avoided this issue. However since you are variable substitution you cannot go that route without more changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM